Convert bool to bool? in @Html.CheckBoxFor() call in ASP.NET MVC

时间:2018-03-25 19:14:57

标签: c# asp.net-mvc

I am attempting to retrieve the bool value from the CheckBox and assign it to the variable m.IsAdministrator, which is of type bool?:

@Html.CheckBoxFor(m => (bool?)(m.isAdministrator))

However, this syntax gives an error. How can I assign the bool CheckBoxFor return value to a bool??

1 个答案:

答案 0 :(得分:-1)

@Html.CheckBoxFor(m => m.isAdministrator.Value)

And be sure to check that it has value before or you'll get exception.

However, using nullable in view model is not a best practice.