Asp.net mvc 4 验证由复选框创建的输入隐藏字段

Asp.net mvc 4 验证由复选框创建的输入隐藏字段,asp.net-mvc-4,Asp.net Mvc 4,模型 [Display(Name = "Remember me?")] public bool RememberMe { get; set; } 看法 创建一个额外字段 <input data-val="true" data-val-required="The Remember me? field is required." id="RememberMe" name="RememberMe" type="checkbox" value="true"> <input name=

模型

[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
看法

创建一个额外字段

<input data-val="true" data-val-required="The Remember me? field is required." id="RememberMe" name="RememberMe" type="checkbox" value="true">
<input name="RememberMe" type="hidden" value="false">
服务器错误

 Server Error in '/' Application.

String was not recognized as a valid Boolean.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.FormatException: String was not recognized as a valid Boolean.

Source Error: 


Line 29:             </li>
Line 30:             <li>
Line 31:                 @Html.CheckBoxFor(m => m.RememberMe)
Line 32:                 @Html.LabelFor(m => m.RememberMe, new { @class = "checkbox" })
Line 33:             </li>

Source File: d:\Projects\MVCTestProject\MVCTestProject\Views\Account\Login.cshtml    Line: 31 

Stack Trace: 

因为您使用了带有签名的强类型@Html.CheckBoxFor helper

public static MvcHtmlString CheckBoxFor<TModel>(this HtmlHelper<TModel> htmlHelper,Expression<Func<TModel, bool>> expression)

你的问题是什么?我想他关心的是有人在RememberMe字段上手动发布随机值,并用问题更新。。是的,作为漏洞和最佳实践的一部分,我需要验证该字段。。。没有死亡屏幕!!你什么时候有黄色屏幕?如果该值不是true或false,则ModelState.IsValid将返回false,并且会为属性添加ModelState.Error-值xxx对RemberMe无效。不会引发异常,在返回视图之前,您可以在服务器上轻松地检查它。有很多方法。要仅检查该属性-if ModelState.containskeyremememberme{ModelState[RememberMe].Errors.Clear;}或要获取所有错误,可以使用var Errors=ModelState.Keys.Wherek=>ModelState[k].Errors.Count>0.Selectk=>new{propertyName=k,errorMessage=ModelState[k].Errors[0].errorMessage};。但正如我所说,这意味着有人在篡改你的网站,所以最好还是把他们返回到错误页面。
[HttpPost]
[AllowAnonymous]
//[ValidateAntiForgeryToken]
public ActionResult Login(LoginViewModel model, string returnUrl)
{
    //Logic to verify user
    //incase of any issue append modelstate.addmodelerror()
     return View(model);
}
 Server Error in '/' Application.

String was not recognized as a valid Boolean.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.FormatException: String was not recognized as a valid Boolean.

Source Error: 


Line 29:             </li>
Line 30:             <li>
Line 31:                 @Html.CheckBoxFor(m => m.RememberMe)
Line 32:                 @Html.LabelFor(m => m.RememberMe, new { @class = "checkbox" })
Line 33:             </li>

Source File: d:\Projects\MVCTestProject\MVCTestProject\Views\Account\Login.cshtml    Line: 31 

Stack Trace: 
public static MvcHtmlString CheckBoxFor<TModel>(this HtmlHelper<TModel> htmlHelper,Expression<Func<TModel, bool>> expression)
[HttpPost]
public ActionResult Login(LoginViewModel model, string returnUrl)
{
  if (!ModelState.IsValid)
  {
    // This will be hit if the checkbox is unchecked and the value of
    // the RememberMe hidden input is not "true" or "false"
    if (ModelState.ContainsKey("RememberMe"))
    {
      ModelState["RememberMe"].Errors.Clear(); } // Remove the model state error
      model.RememberMe = false; // set default when returning the view
    }
  }
  ....
  return View(model);
}