我使用了jqueryui对话框来实现更改密码:
以下是Controller方法,它返回Json for Ajax Request:
[HttpPost]
public ActionResult ChangePassword(ChangePassword password)
{
try
{
if (ModelState.IsValid)
{
tblUser t_User = db.tblUsers.Find(password.UserId);
if (t_User != null)
{
t_User.Password = PC.Utils.Security.GetMD5Hash(password.NewPassword);
db.SaveChanges();
}
RedirectToAction("Index", "Home");
}
ModelState.AddModelError("", "The Passwords do not match.");
if (Request.IsAjaxRequest())
{
return Json(password, JsonRequestBehavior.AllowGet);
}
return PartialView(password);
}
catch (Exception ex)
{
throw ex;
}
}
我正在为此操作发出一个ajax请求,如下面的DOM ready函数中所示:
// initialize Change Password Dialog
$('#Div_Change_Password').dialog({
autoOpen: false,
width: 400,
resizable: false,
modal: true,
position: "center",
open:function (event, ui) {
$(this).load("@Url.Action("ChangePassword", new { id= @Model.UserId })")
},
buttons: {
"Submit" : function ()
{
$.ajax({
type:'POST',
url:'@Url.Action("ChangePassword", new { id= @Model.UserId })',
data: $('#Div_Change_Password:input').serialize(),
success: function (xml, status, error) {
debugger
},
error: function (xml, status, error) {
debugger
$('#Div_Change_Password').html
('<p><strong>Error Code:</strong> '+status+'</p><p><strong>Explanation:</strong> '+error+'</p>');
}
});
},
"Cancel": function () {
$(this).dialog("close");
}
}
});
// Change Password - clicked
$('#Link_Change_Password').click(function () {
$("#Div_Change_Password").dialog('open');
});
当我提交带有空白输入的对话框表单时,我希望调用error()函数,但是,调用success()函数,并且不显示验证消息。有什么我想念的吗?