2012-06-13 24 views
8

Bir ASP.NET MVC3 uygulaması var ve kullanıcı benim çapa etiketi tıkladığında, ben bir eyleme verilerin 3 adet göndermek istediğiniz jquery ile veri göndermebir MVC denetleyicisi

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

My eylem:

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

adet endişe im olan DosyaAdı ve Descriptio javascript İşlemimi çağırmak n bu loooooong olabilir ve ben bir url şöyle görünmesini istemiyorum çünkü:

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

Nasıl bir sorgu dizesi gibi göndermek zorunda kalmadan benim eylem benim verilerde gönderebilirim? Teşekkürler

+0

sen yapma deneyin vermedi türüyle $ .ajax: 'POST'? –

+0

hayır ... hızlı bir örnek verebilir misiniz? – BoundForGlory

+0

@David zaten yaptı :), aşağıya bakın. –

cevap

16

Sen jQuery $ .ajax yöntemini kullanabilir:

o hala çalışır halde
<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> 

, Size Kontrolör yöntemini değiştirmek emin olun:

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

Ajax $.post aracılığıyla ya da [HttpPost] özniteliğiyle bir eylem gerçekleştirerek formun tam bir gönderisini yapabilirsiniz.

+0

Bu, bir biçim değil, görünümün ayarlanma şeklidir, kullanıcı başkalarından bağımsız olarak birden çok şey yapabilir, bu nedenle bir form gerekmez. Önerdiğin ikinci şeyin hızlı bir örneğine ihtiyacım var. – BoundForGlory

0

olarak eyleminizi beyan bir SONRASI

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

görünmez bir HTML formu oluşturun:

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

formu doldurun ve JS ile teslim:

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

    document.editDescriptionForm.submit(); 
} 
İlgili konular