我正在构建一个表单,并在visual studio中使用了基于我的模型的默认脚手架选项。我需要的是在加载和禁用视图时默认选中复选框。 I.E我想阻止用户更改复选框状态。
视图代码如下所示:
@model Models.Checkertest
@{
ViewBag.Title = "Test";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Test</h2>
@using (Html.BeginForm("Test", "AccountManagement", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>User Creation</h4>
<div class="form-group">
@Html.Label("Enable Bypass", htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-4">
<div class="checkbox">
@Html.EditorFor(model => model.box1)
@Html.ValidationMessageFor(model => model.box1, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
}
答案 0 :(得分:1)
在控制器的action方法中,在模型上将box1
属性设置为true。它看起来像这样:
var model = new Checkertest {box1 = true};
return View(model);
要使复选框只读,请在其上设置readonly
属性,如下所示:
@Html.EditorFor(model => model.box1, new {@readonly = "readonly"})