2012-06-13 28 views
8

我有一個ASP.NET MVC3應用程序,當用戶點擊我的錨標記,我想送3個數據的一個動作:發送與jQuery數據的MVC控制器

<a onclick='editDescription(<#= DocID,FileName,Description #>)'></a> 

這是的JavaScript調用我的行動:

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,因爲這些都可以loooooong,我不希望URL出現像這樣:

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

我怎麼能在我的數據發送到我的動作,而無需把它像一個查詢字符串?謝謝

+0

你嘗試做一個$就與類型:「POST」? –

+0

不...可以提供快速樣本嗎? – BoundForGlory

+0

@大衛已經做到了:),見下文。 –

回答

16

你可以使用jQuery $ .ajax方法:

<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

如果您喜歡通過ajax $.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(); 
}