【发布时间】:2018-12-07 00:25:36
【问题描述】:
我有一个不只更新部分视图的部分视图,它会重定向到整个页面的操作。
在部分视图 _registerAccount.cshtml 中,它包含一个注册的发布请求,该请求是通过 Ajax 在名为 _registerAccount 的视图中发出的,如下所示:
@model LoginDemo.Models.LdapAccountModel
@using (Ajax.BeginForm("RegisterLdapAccount", "Account", new AjaxOptions
{
HttpMethod = "Post",
InsertionMode = InsertionMode.Replace
}))
{
@Html.ValidationSummary()
@Html.LabelFor(x => Model.Email) @Html.TextBoxFor(x => Model.Email, new { id = "reg-ytu-email" })
<br />
@Html.LabelFor(x => Model.Password) @Html.PasswordFor(x => Model.Password, new { id = "reg-pw" })
<input type="submit" value="Register" class="btn" />
}
在父页面,Register.cshtml 我有:
@model LoginDemo.Models.RegisterModel
...
<div class="panel hide register-type register-ytu">
@Html.Partial("_registerAccount")
</div>
在 Account Controller 中,ajax 请求转到 RegisterAccount 操作:
[HttpPost]
public async Task<ActionResult> RegisterLdapAccount(LdapAccountModel model)
{
if (model.exists()) // pseudo code
{
return Json(new {foo: "bar"}); // return address to redirect to
}
else
{
ModelState.AddModelError("", "You must have a valid account.");
return PartialView(model); // return error
}
}
我遇到的问题是,无论返回什么都会清除整个页面,而不是只更新父页面中的部分内容。也就是说,如果成功返回JSON只返回JSON,如果失败返回部分视图只返回部分。
n.b.我的 web.config 中有 <add key="UnobtrusiveJavaScriptEnabled" value="true" />。
【问题讨论】:
-
你错过了
UpdateTargetId吗? -
如果在控制器返回中包含局部视图的名称会发生什么?喜欢:
return PartialView("_PartialView", model)? -
按照建议添加了 UpdateTargetID 和 Partial 视图返回 - 都不起作用(一起或单独)
标签: ajax asp.net-mvc