我在使用最新的 mvc 期货 BeginForm 扩展时遇到问题。我已经禁用了客户端验证,因为我已经读到它可能仍然存在错误。基本上第一次发布模型时为空,第二次(以及之后的任何时间)它都会发布模型。
如果我将它切换回 @using(Html.BeginForm()) 或 @using(Html.BeginForm("action", "controller" 等,它会起作用,但我宁愿使用强类型版本。这是我的代码:
控制器
[HandleError]
public class HomeController : BaseServiceController<IUserService>
{
public HomeController(IUserService service, IMapper mapper, ICustomPrincipal user)
: base(service, mapper, user)
{}
[AllowAnonymous]
[HttpGet]
public ActionResult Logon(string ReturnUrl)
{
LogonModel model = new LogonModel() { ReturnUrl = ReturnUrl };
return View(model);
}
[AllowAnonymous]
[HttpPost]
public ActionResult Logon(LogonModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
SfUser sfUser = service.Logon(model.UserName, model.Password);
if (sfUser == null)
{
ModelState.AddModelError("General", "Username or Password incorrect");
return View(model);
}
Response.Cookies.Add(TicketMaster.setAuthCookie(new CustomPrincipalSerializeModel(sfUser)));
return Redirect(model.ReturnUrl);
}
看法
@model LogonModel
@{
ViewBag.Title = "Logon";
}
//@using(Html.BeginForm()) //Works
@using(Html.BeginForm<HomeController>(c => c.Logon(Model)))
{
@Html.HiddenFor(m => m.ReturnUrl)
<div class="editor-label">
@Html.LabelFor(m => m.UserName)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</div>
<br />
<p>
<input type="submit" value="Login" /><br />
@Html.ValidationSummary(true)
</p>
}
如果它总是发布一个空模型,我可以理解这个问题,但只在第一次发布?快把我逼疯了。