控制器獲取行動
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();
}
嘿, 視圖的本質(這是一個任務)需要一個大的複雜視圖模型。因此,頁面上沒有單一表格,而是一系列表格和顯示部分。 是否可以在不使用表單的情況下序列化整個模型? – EverythingGeek
無論我做什麼,它總是發佈到獲取操作。 – SteveCav