2012-06-13 4 views
8

내가 ASP.NET MVC3 응용 프로그램을 사용자가 내 앵커 태그를 클릭 할 때, 내가 작업 데이터의 3 개를 보낼에 jQuery로 데이터를 보내기MVC 컨트롤러

function editDescription(docId,fileName,description) { 
    var url = "@Url.Content("~/OrderDetail/_EditDescription/")" + docId+'/'+ 
    fileName + '/' + description; 
    //do the rest} 

내 행동 :

public ActionResult _EditDescription(string id,string filename, string descritpion) 

조각이 걱정 메신저는 파일 이름과 Descriptio 자바 스크립트는 내 행동을 호출 N이이 기다란 될 수 있으며 내가 URL을과 같이 표시 할 해달라고 때문에 :

http://localhost/OrderDetail/_EditDescription/123/some long filename.pdf/this is a long description for the name 

어떻게 쿼리 문자열처럼 보내지 않고도 내 행동에 내 데이터를 통해 보낼 수 있습니까? 감사합니다

+0

당신이 만드는 시도 않은 유형 $ 아약스 : 'POST'? –

+0

아니요 ... 빠른 샘플을 제공 할 수 있습니까? – BoundForGlory

+0

@David가 이미 해냈습니다 :). 아래를 참조하십시오. –

답변

16

을 당신 jQuery $ .ajax 메소드를 사용할 수 있습니다.

그것은 여전히 ​​작동하더라도 16,
<div id="what-I-want-updated"> 

    <input id="whatever-the-id-is" type="text" value="@Model.ID" /> 
    <br /> 
    <input id="whatever-the-filename" type="text" value="@Model.Filename" /> 
    <br /> 
    <input id="whatever-the-description" type="text" value="@Model.Description" /> 
    <br /> 
    <button id="whatIsClicked">Update!</button> 

</div> <!-- /#what-I-want-updated --> 

<script> 

    // You're probably clicking something to initiate update 
    var $whatIsClicked = $('#whatIsClicked'); 

    // .live persists on the page even after other ajax calls 
    // So when the thing is clicked 
    $whatIsClicked.live('click', function() { 

     // Grab the information needed to update 
     var theId = $('#whatever-the-id-is').val(); //Or it could be .text() 
     var theFilename = $('#whatever-the-filename').val(); 
     var theDescript = $('#whatever-the-description').val(); 

     // Let's edit the description! 
     $.ajax({ 
     type: "POST", 
     url: "OrderDetail/_EditDescription", // the method we are calling 
     contentType: "application/json; charset=utf-8", 
     data: {id: theId, filename: theFilename, description: theDescript}, 
     dataType: "json", 
     success: function (result) { 
      alert('Yay! It worked!'); 
      // Or if you are returning something 
      alert('I returned... ' + result.WhateverIsReturning);      
     }, 
     error: function (result) { 
      alert('Oh no :('); 
     } 
    }); 
    }); 
</script> 

, 당신이 당신의 컨트롤러 방법을 변경해야합니다 :

[HttpPost] 
public ActionResult _EditDescription(string id, string filename, string descritpion) 
2

아약스 $.post을 통해 또는 [HttpPost] 속성을 사용하여 원하는 경우 양식의 전체 게시물을 수행 할 수 있습니다.

+0

이것은보기가 설정되는 방식에 더하기 양식이 아니기 때문에 사용자는 다른 사람과 독립적으로 여러 가지 작업을 수행 할 수 있으므로 양식은 필요하지 않습니다. 내가 제안한 두 번째 샘플에 대한 빠른 샘플이 필요해. – BoundForGlory

0

같은 조치를 선언하라 POST

[HttpPost] 
public ActionResult _EditDescription(string docId, string filename, string description) 

보이지 않는 HTML 양식을 만듭니다

<form action="@Url.Content("~/OrderDetail/_EditDescription/")" method="post" name="editDescriptionForm"> 
    <input type="hidden" name="docId" /> 
    <input type="hidden" name="fileName" /> 
    <input type="hidden" name="description" /> 
</form> 

양식을 작성하고 JS와 함께 제출

function editDescription(docId, fileName, description) { 
    document.editDescriptionForm.docId = docId; 
    ... 

    document.editDescriptionForm.submit(); 
}