MVC 4 Custom template for bool (razor)

后端 未结 4 1551
夕颜
夕颜 2021-02-07 09:37

I am using the twitter bootstrap framework, so to get the EditorFor and DisplayFor methods to output what I need, I created custom templates for each of the types like string, t

4条回答
  •  无人及你
    2021-02-07 10:17

    I would not do it that way. If the value can be null, I would make sure that your editor template has nullable boolean as the model type. So your editor template (in Views\Shared\EditorTemplates\Boolean.cshtml) would be:

    @model Boolean?
    
    @Html.CheckBox("", Model.HasValue && Model.Value)
    

    And then in the razor of your main view, you could have:

提交回复
热议问题