Asp.net mvc 3 如何将其设置为复选框?
因此,我有这样的看法:Asp.net mvc 3 如何将其设置为复选框?,asp.net-mvc-3,razor,Asp.net Mvc 3,Razor,因此,我有这样的看法: @model Tuple<LocApp.Models.Location, LocApp.Models.Service> @{ ViewBag.Title = "Create"; } <h2>Create</h2> <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
@model Tuple<LocApp.Models.Location, LocApp.Models.Service>
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Location</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Item1.name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Item1.name)
@Html.ValidationMessageFor(model => model.Item1.name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Item1.active)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Item1.active)
@Html.ValidationMessageFor(model => model.Item1.active)
</div>
@Html.CheckBoxFor(model => model.Item2.id)
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@模型元组
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm()){
@Html.ValidationSummary(true)
位置
@LabelFor(model=>model.Item1.name)
@EditorFor(model=>model.Item1.name)
@Html.ValidationMessageFor(model=>model.Item1.name)
@LabelFor(model=>model.Item1.active)
@EditorFor(model=>model.Item1.active)
@Html.ValidationMessageFor(model=>model.Item1.active)
@CheckBoxFor(model=>model.Item2.id)
}
@ActionLink(“返回列表”、“索引”)
这行代码是:@Html.CheckBoxFor(model=>model.Item2.id)
IMO应该通过,并且为数据库中的每个服务创建一个基于id的复选框,这样我就可以保存id。然而,显然我必须传入一个布尔值?我只有一个model.Item2.active
想法?复选框只能具有布尔状态:选中或未选中。您的模型必须使用布尔值true或false设置复选框状态。如果希望复选框充当将Id保存到数据库的机制,则需要编写业务逻辑来检查控制器操作中是否选中了复选框。如果选中该复选框,请写入Id,否则不要写入Id。您应该选中bool model.Item1.checked(或active、enabled等),然后使用该选项而不是model.Item1.Id,它将跟踪该项的标志是打开还是关闭
然后,当他们发布表单时,您可以循环(或使用LINQ)所有项目,并保存每个项目的id。checked=true。好的,我了解业务逻辑方面,所以我假设我必须在我的模型中创建选中的bool?如果要从数据库中加载复选框状态,则需要向模型中添加选中状态。否则,您可以在表单值上循环,查看表单POST.ok上是否选中了特定的复选框。这是可行的,如果我可以扩展一下,我有6或7个服务,我只有一个复选框。。。。这是另一个问题,还是你也能回答?