为什么表单为已检查的单选按钮而不是true或false返回null值?

时间:2014-09-10 09:05:56

标签: radio-button asp.net-mvc-5 mvc-editor-templates

我的模型中有一个布尔字段,我正在使用编辑器模板,以显示是和否的单选按钮。

@Html.EditorFor(model => model.Disruption)

@{
  Dictionary<string, object> yesAttrs = new Dictionary<string, object>();
  Dictionary<string, object> noAttrs = new Dictionary<string, object>();

  yesAttrs.Add("id", ViewData.TemplateInfo.GetFullHtmlFieldId("") + "Yes");
  noAttrs.Add("id", ViewData.TemplateInfo.GetFullHtmlFieldId("") + "No");

  if (Model.HasValue && Model.Value)
  {
      yesAttrs.Add("checked", "checked");
  }
  else if (Model.HasValue && !Model.Value)
  {
      noAttrs.Add("checked", "checked");
  }
}

@Html.RadioButtonFor(x => x, "true", yesAttrs) 
<label for="@(ViewData.TemplateInfo.GetFullHtmlFieldId(""))Yes">Yes</label>
@Html.RadioButtonFor(x => x, "false", noAttrs) 
<label for="@(ViewData.TemplateInfo.GetFullHtmlFieldId(""))No">No</label>

在浏览器上的开发人员工具中使用控制台查看器我可以看到,当我选中“是”时,值为true,当我选中“否”时,值为false,但是当我单击“提交”时,插入失败,因为列不能包含NULL值。为什么在这种情况下,当相同的代码似乎在其他字段上工作时,真值或假值没有正确传递?

0 个答案:

没有答案