我遇到了Asp.net mvc 3 razor部分页面更新的问题,我无法通过
我有这个_LogOn局部视图,其中包含2个文本框电子邮件和密码以及用于登录目的的按钮连接:
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div>
<p>
<input id="submitConnexion" type="submit" value="Connexion" />
<a id="lnForgetPwd"> Mot de passe oublié</a>
</p>
}
这里是控制器:GET控制器只不过是抓取_Logon partialV视图并将其交给jquery来更新一个名为Div-2的div。
// GET: Account/Logon
public ActionResult LogOn()
{
return PartialView("_LogOn");
}
POST控制器应检查密码是否正确,如果是,如果登录/密码正常则返回Json值为true,否则会刷新Div-2错误。
// POST: Account/Logon
[HttpPost]
public ActionResult LogOn(UserLogon model)
{
bool result = false;
if (ModelState.IsValid)
{
UserManager userManager = new UserManager();
string password = userManager.GetUserPassword(model.Email);
if (string.IsNullOrEmpty(password))
{
ModelState.AddModelError("", "Mot de passe ou email incorrect.");
result = false;
}
if (model.Password == password)
{
FormsAuthentication.SetAuthCookie(model.Email, false);
result = true;
}
}
//return Json(new { JsResult = result });
return PartialView("_Logon", model);
}
最后是Ajax Jquery代码
// Ajax call POST for login form button submitConnexion
$("#submitConnexion").live('click', function () {
var email = $("#div-2 #Email").val();
var password = $("#div-2 #Password").val();
var data = { Email: "hello", Password: password };
alert(email + password);
$.ajax({
url: "/Account/LogOn",
type: "POST",
//data: JSON.stringify(data),
//contentType: 'application/json; charset=utf-8',
success: function (result) {
alert("Hello");
$("#div-2").empty();
$("#div-2").html(result).hide().fadeIn(300);
}
});
});
// Ajax call POST for login form button
它不能解决2个问题
因此,partialView显示在新的网页上,而不是显示在Div-2
中谢谢
答案 0 :(得分:1)
一次一个问题
'@Url.Action("ActionMethodName", "ControllerName")'
,您完成的操作不准确。dataType: "json"
,这可以确保你可以返回json而不是部分视图答案 1 :(得分:0)
你有没有理由不使用Ajax.BeginForm而不是Html.BeginForm?你应该可以这样做:
@using (Ajax.BeginForm("LogOn", "Account", new AjaxOptions { UpdateTargetId = "div-2", OnBegin = "hideMessage", OnSuccess = "fadeInMessage" }))
hideMessage和fadeMessage是JS函数隐藏和淡入你的div。
答案 2 :(得分:0)
你应该挂钩表单的提交事件而不是单击按钮的事件,在提交处理程序中你必须告诉浏览器不要通过简单地返回false或使用preventDefault()
之类的
$('form').live('submit',function(ev){
ev.preventDefault();//to stop normal form post
var email = $("#div-2 #Email").val();
var password = $("#div-2 #Password").val();
var data = { Email: "hello", Password: password };
alert(email + password);
$.ajax({
url: "/Account/LogOn",
type: "POST",
data: JSON.stringify(data),
contentType: 'application/json; charset=utf-8',
dataType:'json',
success: function (result) {
alert("Hello");
$("#div-2").empty();
$("#div-2").html(result).hide().fadeIn(300);
}
});
//return false; you can simply uncomment this line and remove ev.preventDefault() to get same result
});