复选框默认为已选中

时间:2015-09-02 13:15:29

标签: c# asp.net-mvc

如何在View中创建新实体时设置该选项默认情况下,复选框始终设置为选中(true)?

在viewmodel中:

public bool AutoBlock {get;set;}

在视图中我渲染它:

<div class="form-group">
            @Html.LabelFor(model => model.AutoBlock, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.AutoBlock, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.AutoBlock, "", new { @class = "text-danger" })
            </div>
        </div>

1 个答案:

答案 0 :(得分:1)

如果它是一个viewmodel,那么只需在构造函数中将值设置为true。

public class MyViewModel
{
    public MyViewModel()
    {
        AutoBlock = true;
    }

    public bool AutoBlock {get;set;}
}