我目前正在研究一個ASP.NET MVC 4項目作爲實習生,我試圖實現一個管理面板。目標是顯示網格上的所有用戶(MVC.GRID)並在同一頁面上編輯它們。 我已經設法顯示網格上的所有用戶,並且一旦選擇了用戶,它將顯示網格下方的信息並將其放入表單中(通過ajax/jquery)。Asp.net MVC 4 Ajax.BeginForm提交+ mvc.grid - 頁面重新加載
問題是:窗體驗證顯示在新頁面上,而不是網格所在的頁面上。我不知道爲什麼..
以下是我的代碼。
這是形式放置:
<div id="order-content">
<p class="muted">
Select a user to see his or her information
</p>
</div>
形式本身(局部視圖「_UserInfo):
@using (Ajax.BeginForm("EditUser", "Admin", FormMethod.Post,
new AjaxOptions
{
InsertionMode = InsertionMode.Replace,
HttpMethod = "POST",
UpdateTargetId = "order-content"
}))
{
@Html.Bootstrap().ValidationSummary()
@Html.Bootstrap().ControlGroup().TextBoxFor(x => x.Id)
@Html.Bootstrap().ControlGroup().TextBoxFor(x => x.Name)
@Html.Bootstrap().ControlGroup().TextBoxFor(x => x.Password)
@Html.Bootstrap().SubmitButton().Text("Opslaan").Style(ButtonStyle.Primary)
}
JQuery的向用戶顯示信息一旦行被選擇:
$(function() {
pageGrids.usersGrid.onRowSelect(function (e) {
$.post("/Admin/GetUser?id=" + e.row.Id, function (data) {
if (data.Status <= 0) {
alert(data.Message);
return;
}
$("#order-content").html(data.Content);
});
});
});
我的AdminController:
[HttpPost]
public JsonResult GetUser(int id)
{
var user = _UserService.Get(id);
var input = _EditInputMapper.MapToInput(user);
if (user == null)
return Json(new { Status = 0, Message = "Not found" });
return Json(new { Content = RenderPartialViewToString("_UserInfo", input) });
}
[HttpPost]
public ActionResult EditUser(AdminUserEditInput input)
{
if (ModelState.IsValid)
{
// todo: update the user
return View();
}
// This is where it probably goes wrong..
return PartialView("_UserInfo",input);
}
任何人都可以看到我的代碼有什麼問題嗎?
謝謝。
在「order-content」中,您顯示的是「_UserInfo」。並且這個_UserInfo與模型數據綁定。所以模型驗證應該顯示在「訂單內容」中。不是嗎?或者我不理解你的問題? – kandroid
是的。但是當我提交空字段時,頁面會重新加載並用驗證顯示我的表單,但是我的網格消失。看起來mvc創建一個只有部分視圖的新頁面。 – Nixxing
檢查ajax提交,你可以使用Firebug來檢查...該頁面不應該在ajax提交的情況下重新加載 – kandroid