2010-08-06 65 views
5

我試圖用jQuery提交表單數據。我正在使用ASP.NET WebMatrix。在.cshtml文件中我有從ASP.NET網頁(WebMatrix)jQuery發佈

@{ 
    // other code 
    if(IsPost) 
    { 
     var item = new Item(); 
     item.Title = Request.Form["title"]; 
     item.Description = Request.Form["description"]; 

     // aditional code here 
    } 
} 
<script type="text/javascript"> 
    $(document).ready(function(){ 
     $("form#itemForm").submit(function(){ 
      $.post("form.cshtml", { 
        title: $("#title").val(), 
        description: $("#description").val(), 
        price: $("#price").val()}, 
        function(data){ 
        }, 
        "json"); 
     }) 
    }); 
</script> 
<form> 
<!-- html form here --> 
</form> 

如何將值從表單傳遞到Request.Form對象?我怎麼能比json迴應html?

回答

0

值通過jQuery.post()傳遞給Request.Parameters。

6

一個更好的方法是讓jQuery使用$(this).serialize()發佈表單數據,而不是構建一個包含所有值的對象。在那之後,yah,Request [「title」]等會得到已發佈的值。

+0

是的,我後來也發現了。不管怎麼說,還是要謝謝你! – zigomir 2010-08-12 18:10:09