我默认需要选中一个复选框:
我尝试了所有这些,没有检查我的复选框 -
@Html.CheckBoxFor(m => m.AllowRating, new { @value = "true" })
@Html.CheckBoxFor(m => m.AllowRating, new { @checked = "true" })
@Html.CheckBoxFor(m => m.AllowRating, new { @checked = true })
@Html.CheckBoxFor(m => m.AllowRating, new { @checked = "checked"})
Run Code Online (Sandbox Code Playgroud) 我有一个剃刀视图(Framework 4.5,MVC 5)和一个html输入类型=复选框,其值等于模型布尔值,但它不是true或false,而是绑定"value".
这是我的代码:
for (int index = 0; index < Model.Item1.Locations.Length; index++)
{
WSTupleOfInt32StringStringBoolean location = Model.Item1.Locations[index];
<div class="editor-field">
<input id="@string.Format("presentation.ServiceInfoes[{1}].Locations[{0}].Key", index, Model.Item2)" type="checkbox" value="@location.ThirdValue" name="@string.Format("presentation.ServiceInfoes[{1}].Locations[{0}].ThirdValue", index, Model.Item2)" />
</div>
<div class="editor-label">
<label for="@string.Format("presentation.ServiceInfoes[{1}].Locations[{0}].Key", index, Model.Item2)">@location.FirstValue</label>
@if (!string.IsNullOrEmpty(location.SecondValue))
{
<a href="@string.Format("//maps.google.com/?q={0}", location.SecondValue)" target="_blank"><img alt="@location.SecondValue" src="@string.Format("//maps.google.com/maps/api/staticmap?center={0}&markers=color:blue|{0}&zoom=15&size=64x64&sensor=false", location.SecondValue)" /></a>
}
</div><br />
}
Run Code Online (Sandbox Code Playgroud)
这location.ThirdValue
是布尔属性,对该属性进行调试它很好..但在HTML中我得到value="value"
而不是value="false"
.
发生了什么?
我有一个与用户关联的项目列表.这是一对多的关系.我希望将整个项目列表传递到视图中,以便他们可以从尚未与之关联的项目中进行选择(并且还可以查看已经关联的项目).我想从这些创建复选框.然后我想将所选择的那些发送回控制器以进行关联.我如何传递所有这些列表,包括那些尚未关联的列表,并可靠地将它们传回以进行关联?
这是我首先尝试的,但很明显这不会起作用,因为我基于通过AllItems
集合传入的项目的输入,该集合与用户本身的项目没有任何关联.
<div id="item-list">
@foreach (var item in Model.AllItems)
{
<div class="ui field">
<div class="ui toggle checkbox">
<input type="checkbox" id="item-@item.ItemID" name="Items" value="@item.Active" />
<label for="item-@item.ItemID">@item.ItemName</label>
</div>
</div>
}
</div>
Run Code Online (Sandbox Code Playgroud) 我创建了一个asp.net网站,但此复选框始终为false。为什么会这样呢?
模型:
public string Username { get; set; }
public string Password { get; set; }
public bool Remember { get; set; }
Run Code Online (Sandbox Code Playgroud)
CSHTML:
<div class="form-group">
@Html.Label("Remember me?")
@Html.CheckBoxFor(m => m.Remember)
</div>
Run Code Online (Sandbox Code Playgroud)
该Remember
属性始终为false,如果选中该复选框,则该属性Remember
仍为false。