c# - Asp.net MVC 4 Ajax.BeginForm 提交 + mvc.grid - 页面重新加载

标签 c# jquery ajax asp.net-mvc mvcgrid

我目前正在以实习生的身份从事 ASP.NET MVC 4 项目,我正在尝试实现一个管理面板。目标是在网格 (MVC.GRID) 上显示所有用户并在同一页面上编辑它们。 我已经设法在网格上显示所有用户,一旦选择了用户,它就会在网格下方显示信息并将其放入表单中(通过 ajax/jquery)。

问题是:表单验证显示在新页面上,而不是网格所在的页面上。我不知道为什么..

下面是我的代码。

这是放置表单的地方:

<div id="order-content">
  <p class="muted">
    Select a user to see his or her information
  </p>
</div>

表单本身(部分 View “_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);
  });
});
});

我的管理 Controller :

[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);
}

任何人都可以看到我的代码有什么问题吗?

谢谢。

最佳答案

当 ModelState 有效并且您返回 View() 时,这个完整 View 是否嵌入到订单内容中?我怀疑不是,如果是这样,那是因为没有发送 ajax 请求。您可能没有包含 jquery.unobtrusive-ajax js 文件

关于c# - Asp.net MVC 4 Ajax.BeginForm 提交 + mvc.grid - 页面重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19112475/

相关文章:

c# - System.IO.FileNotFoundException : myCSharpDemoCalc\work\MyCSharpDemoCalc. j4n.dll

c# - 在 C# 中,public、private、protected 和没有访问修饰符有什么区别?

javascript - 如何在页面加载时禁用 Anchor(a ) 标记或(默认情况下禁用)并使用 jquery 或 Javascript 启用它?

javascript - 在元素之间移动 - 保持显示 : block attribute

javascript - ajax 发布到 node.js Express 应用程序?

c# - 是否有任何通用的 Parse() 函数可以使用解析将字符串转换为任何类型?

c# - 为什么 C# 动态解析内部属性而不是方法?

javascript - 将鼠标悬停在非父级/非同级 div 上时,div 背景图像会发生变化

javascript - 使用 javascript 和 ajax 实时验证用户名

php - Laravel - 格式错误的 UTF-8 字符,可能编码不正确