【发布时间】:2018-01-16 18:09:42
【问题描述】:
我已将部分视图放入模式中以更新密码,如下所示:
<div class="modal fade" id="modalPassword" tabindex="-1" role="dialog"
aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-body">
<div class="modal-content">
<div id="message"></div>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Change Password</h4>
</div>
<div class="modal-
<div id="passwordForm">
@{
@Html.Action("ChangePassword","Account");
}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
这是我的部分观点:
@model WebApplication1.Models.ViewModel.ChangeUserPassword
@{
Layout = null;
}
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<form id="form">
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset id="submitPasswordForm">
<div class="col_full">
@Html.LabelFor(model => model.OldPassword, htmlAttributes: new { @class = "capitalize t600" })
@Html.TextBoxFor(model => model.OldPassword, null, new { @class = "sm-form-control", id = "txtOldPassword" })
@Html.ValidationMessageFor(model => model.OldPassword)
</div>
<div class="col_full">
@Html.LabelFor(model => model.ChangedPassword, htmlAttributes: new { @class = "capitalize t600" })
@Html.TextBoxFor(model => model.ChangedPassword, null, new { @class = "sm-form-control", id = "txtChangedPassword" })
@Html.ValidationMessageFor(model => model.ChangedPassword)
</div>
<div class="col_full">
@Html.LabelFor(model => model.ConfirmPassword, htmlAttributes: new { @class = "capitalize t600" })
@Html.TextBoxFor(model => model.ConfirmPassword, null, new { @class = "sm-form-control", id = "txtConfirmPassword" })
@Html.ValidationMessageFor(model => model.ConfirmPassword)
</div>
<div class="modal-footer">
<button type="button" class="btn btn-warning" data-dismiss="modal">Cancel</button>
<input type="submit" value="Save Changes" class="btn btn-primary" id="btn_save_password" />
</div>
</fieldset>
</form>
当我点击“btn_save_password”时,我会像这样调用 onclick 事件:
$("#btn_save_password").click(function (event) {
event.preventDefault();
var data = $("#submitPasswordForm").serialize();
$.ajax({
type: "POST",
url: "@Url.Action("ChangePassword", "Account")",
data: data,
success: function (result) {
$("#passwordForm").empty();
//$("div").remove("#passwordForm");
addHtml(result);
},
error: function () {
$("#passwordForm").html("Error occured");
}
});
});
function addHtml(htmlString) {
$("#msg").html(htmlString);
}
然后它调用我的控制器“ChangePassword”中的一个方法
[Authorize]
public ActionResult ChangePassword()
{
return PartialView();
}
[HttpPost]
public ActionResult ChangePassword(ChangeUserPassword password)
{
if (ModelState.IsValid)
{
var cookie = HttpContext.Request.Cookies["Sys_user_id"];
var um = new UserManager();
if (cookie != null && um.GetAccountPassword(Convert.ToInt32(cookie.Value), password.OldPassword))
{
um.ChangeUserPassword(password, Convert.ToInt32(cookie.Value));
}
else
{
ModelState.AddModelError("","Wrong current password");
}
}
else
{
ModelState.AddModelError("","Error");
}
return View();
}
“ChangePassword”方法调用 PartialView “ChangePassword.html”,如下所示:
[Authorize]
public ActionResult ChangePassword {
return PartialView();
}
我可以在模态框上查看部分视图,并且能够成功更新数据库。但问题是,我希望能够在成功与否时将成功消息或错误消息发送到模式中。提交后,无论是否更新了数据库,刷新页面,modal消失。我希望能够将消息放入模态。 非常感谢您的帮助。
编辑 -- 我现在可以在 Modal 中看到验证消息,但它只能工作一次。一旦我再次点击“btn_save_password”,页面就会刷新。
【问题讨论】:
-
它是一个提交按钮,您还没有取消默认操作,因此它同时进行了 ajax 调用和正常提交。而且您显示的最后一个代码 sn-p 没有意义(我假设您的意思是
public ActionResult ChangePassword { ... },但您从未在任何地方调用该方法 - 所有您使用我们@Html.Partial(),而不是@Html.Action()) -
ActionResult ChangePassword 在模态本身上被调用。
-
不是基于你显示的代码!
-
@{ @Html.Partial("ChangePassword", new ChangeUserPassword()); }
您需要阅读第一条评论 -
@Html.Partial() 不会调用服务器方法。为此,您需要使用@Html.Action()。但无论如何,你的那部分代码是无关紧要的。
标签: javascript jquery ajax asp.net-mvc