我想通过ajax更新局部视图,但它不起作用。看看这个模型类:部分视图和ajax
public class LogOnModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
public bool IsLoggedIn { get; set; }
public string ReturnUrl { get; set; }
}
以下几种观点:
@model ITW2012Mobile.ViewModels.LogOnModel
<div id='LogOn' style="background-color: White;">
@using (Ajax.BeginForm("LogOnAjax", "Home", new AjaxOptions { UpdateTargetId = "LogOn", OnSuccess = "logInComplete" }))
{
ITW2012Mobile.ViewModels.LogOnModel m = Model;
@Html.EditorFor(model => model.IsLoggedIn)
@Html.EditorFor(model => model.ReturnUrl)
<div>
@Html.ValidationSummary()
</div>
<div>
@Html.LabelFor(model => model.UserName)
@Html.EditorFor(model => model.UserName)
</div>
<div>
@Html.LabelFor(model => model.Password)
@Html.EditorFor(model => model.Password)
</div>
<div>
<input type="submit" value="Login" />
</div>
}
</div>
与下列的控制器类:
public ActionResult LogOnAjax(LogOnModel model)
{
if (!User.Identity.IsAuthenticated)
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
model.IsLoggedIn = true;
model.ReturnUrl = Url.Action("Index", "Home");
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
// If we got this far, something failed, redisplay form
return PartialView("PartialViewAjaxLogOn", model);
}
else
{
return PartialView("PartialViewLogOut");
}
}
即使用户名/密码是否正确,IsLoggedIn =真实RETURNURL !=空视图显示这些变量的空字段(但调试器显示内部的值)。为什么以及如何使它正确?
注意你上面的评论'//如果我们得到了这么多,失败的东西,重新显示的形式',然而,你没有什么可以在model.returnUrl之后停止处理,所以根据你上面的代码注释是不正确的 – 2012-01-17 00:24:09
没有任何理解... – John 2012-01-17 00:33:33