2012-06-13 25 views
8

Tôi có một ứng dụng ASP.NET MVC3 và khi người dùng nhấp vào thẻ neo của tôi, tôi muốn gửi 3 mẩu dữ liệu đến một hành động:Gửi dữ liệu với jquery để một bộ điều khiển MVC

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

Đây là javascript để gọi hành động của tôi:

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

hành động của tôi:

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

những mảnh im quan tâm đến là FileName và Descriptio n vì chúng có thể được loooooong và tôi không muốn có một địa chỉ để xuất hiện như vậy:

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

Làm thế nào tôi có thể gửi qua dữ liệu của tôi để hành động của tôi mà không cần phải gửi nó như một chuỗi truy vấn? Cảm ơn

+0

cậu cố gắng thực hiện một $ .ajax với kiểu: 'POST'? –

+0

không ... bạn có thể cung cấp mẫu nhanh không? – BoundForGlory

+0

@David đã làm điều đó rồi :), xem bên dưới. –

Trả lời

16

Bạn có thể sử dụng phương thức $ .ajax của jQuery:

<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> 

Mặc dù nó vẫn sẽ làm việc, chắc chắn rằng bạn thay đổi phương thức điều khiển của bạn để:

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

Bạn có thể đăng toàn bộ biểu mẫu nếu bạn muốn thông qua ajax $.post hoặc bằng cách thực hiện hành động với thuộc tính [HttpPost].

+0

Điều này không có trong một biểu mẫu cộng với cách thiết lập chế độ xem, người dùng có thể thực hiện nhiều việc độc lập với người khác, do đó, không cần biểu mẫu. Tôi cần một mẫu nhanh thứ hai mà bạn đề nghị – BoundForGlory

0

Khai báo hành động của bạn như một POST

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

Tạo một hình thức HTML vô hình:

<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> 

Điền vào biểu mẫu và gửi nó với JS:

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

    document.editDescriptionForm.submit(); 
} 
Các vấn đề liên quan