如果你有兩個簡單的形式,你可以用這個形式給出:
創建兩個不同的部分景色。
@model CustomerInfoModel
@using (Ajax.BeginForm("CustomerInfo", "Customer", new AjaxOptions { HttpMethod = "Post", OnBegin = "InfoLoading", OnComplete = "InfoCompleted" }, new { id = "info", @class = "form-horizontal" }))
{
<input type="text" class="form-control" name="Name" id="Name" value="@Model.Name" />
<input type="email" class="form-control" name="Email" id="Email" value="@Model.Email" />
<button type="submit" id="save-info" class="btn-medium red">Save</button>
}
和
@model CustomerPasswordChangeModel
@using (Ajax.BeginForm("CustomerPasswordChange", "Customer", new AjaxOptions { HttpMethod = "Post", OnBegin = "InfoLoading", OnComplete = "InfoCompleted" }, new { id = "change", @class = "form-horizontal" }))
{
<input type="password" class="form-control" name="OldPassword" id="OldPassword" value="" />
<input type="password" class="form-control" name="NewPassword" id="NewPassword" value="" />
<button type="submit" id="save-change" class="btn-medium red" autocomplete="off">Save</button>
}
在你的父視圖,
@Html.Partial("CustomerInfo", Model.CustomerInfo)
和
@Html.Partial("CustomerPasswordChange", Model.CustomerPasswordChange)
在控制器:
[HttpPost]
public ActionResult CustomerInfo([Bind(Include = "Name,Email")] CustomerInfoModel model)
{
if (ModelState.IsValid)
return new Json(new { success=true, message="Updated.", errors=null);
// do you logic
return new Json(new { success=false, message="", errors=getHtmlContent(ModelState.Values.SelectMany(v => v.Errors).ToList(), "ModelError"));
}
[HttpPost]
public ActionResult CustomerPasswordChange([Bind(Include = "OldPassword,NewPassword")] CustomerPasswordChangeModel model)
{
if (ModelState.IsValid)
return new Json(new { success=true, message="Updated.", errors=null);
// do you logic
return new Json(new { success=false, message="", errors=getHtmlContent(ModelState.Values.SelectMany(v => v.Errors).ToList(), "ModelError"));
}
這會做你想做的事情。
注意:getHtmlContent方法只是生成一個錯誤消息以顯示在頁面上。沒什麼特別的。如有需要,我可以分享。
你錯過了這一點。問題是 - 如何更新1表單,如果可能,而不是如何使用ViewData字典。 – 2009-05-05 00:45:50
對不起,我一直在讀你的問題,我不明白你在問什麼。 – Skiltz 2009-05-05 00:54:51
我只是問,如果頁面上有2