2013-02-07 18 views
0

我有我的AJAX調用MVC4 Ajax-通完整的模型到控制器

$.ajax({ 
    type: 'post', 
    url: "/Store/LoadStoreIndex", 
    data: , //Entire Model Here!! 
    dataType: "text", 
    success: function (result) { 
     $('#Postback').html(result); 
    } 
}); 

我需要我的整個模型送回控制器,但經過一番搜索找不到什麼......有人能秀我需要做什麼?

回答

4

控制器獲取行動

public ActionResult Index(YourModel model) 
{ 
    YourModel model = new YourModel(); 

    return View(model); 
} 

查看

@model YourModel 
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "form1" })) 
{ 
    @Html.TextBoxFor(x => x.EmailAddress) 
    @Html.TextBoxFor(x => x.Name) 
    ... 
} 

腳本

$(function() { 
    $('form').submit(function() { 
     if ($(this).valid()) { 
      $.ajax({ 
       url: this.action, 
       type: this.method, 
       // you can post your model with this line 
       data: $(this).serialize(), 
       beforeSend: function() { 

       }, 
       complete: function() { 

       }, 
       success: function (result) { 

       }, 
       error: function() { 

       } 
      }); 
     } 
     return false; 
    }); 
}); 

控制器POST操作

[HttpPost] 
public ActionResult Index(YourModel model) 
{ 
    return View(); 
} 
+1

嘿, 視圖的本質(這是一個任務)需要一個大的複雜視圖模型。因此,頁面上沒有單一表格,而是一系列表格和顯示部分。 是否可以在不使用表單的情況下序列化整個模型? – EverythingGeek

+0

無論我做什麼,它總是發佈到獲取操作。 – SteveCav

相關問題