2
我有問題使用最新的mvc期貨BeginForm擴展。我已經讀取了可能仍然存在的問題,因此我禁用了客戶端驗證。基本上第一次被髮布的模型是空的,第二次(以及隨後的任何時間),它會發布模型罰款。如果我將它切換回@using(Html.BeginForm())或@using(Html.BeginForm(「action」,「controller」等,但我寧願使用強類型的版本。是我的代碼:?BeginForm <TController>不會發布模型的第一次
控制器
[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>
}
我能理解這個問題,如果它總是貼空模型,但只在第一篇文章把我逼瘋