C# 为剃刀中的复选框设置标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17626171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set Label for checkbox in razor
提问by Vignesh
How does one set the label of a checkbox? I looked at some sites and they are using lambda expressions, but I can't understand them. I am new to asp.net MVC.
如何设置复选框的标签?我查看了一些网站,他们使用 lambda 表达式,但我无法理解它们。我是 ASP.NET MVC 的新手。
@{
bool chkVal = false;
}
<li>@Html.CheckBox("chksumma",chkVal,new {@value = "5"})</li>
<li>@Html.LabelFor(, "");
回答by Mark Milford
If you are using a Model you could do:
如果您使用的是模型,则可以执行以下操作:
<li>@Html.CheckBoxFor(f=> f.chksumma)</li>
<li>@Html.LabelFor(f=> f.chksumma)</li>
Then use the attributes TGH pointed out
然后使用TGH指出的属性
otherwise if you don't have a model all you can do for labels is:
否则,如果您没有模型,您可以为标签做的是:
@Html.Label("LabelText")
which prints a standalone label or and craft it if you want it to link with the item
如果您希望它与项目链接,它会打印一个独立的标签或制作它
<label for="chksumma">LAbelText</label>
If you really don't want to hand craft it you can write your own HTML helper as explained here http://develoq.net/2011/how-to-create-custom-html-helpers-for-asp-net-mvc-3-and-razor-view-engine/
如果您真的不想手工制作它,您可以按照此处的说明编写自己的 HTML 帮助程序 http://develoq.net/2011/how-to-create-custom-html-helpers-for-asp-net-mvc -3-and-razor-view-engine/
回答by Simon M?Kenzie
I'm assuming that you want the label to tick the checkbox when you click on it.
我假设您希望标签在单击时勾选复选框。
In this case, the forattribute of the HTML <label>field must point to the ID of the relevant input element.
在这种情况下,forHTML<label>字段的属性必须指向相关输入元素的 ID。
If you're using a model, @Html.CheckBoxForwill generate a checkbox without an ID, so you will need to add an ID to the checkbox, then point your label to the same ID. The easiest way is to replicate the checkbox's name into its ID field using the @Html.NameForhelper method. Here's an example:
如果您使用的是模型,@Html.CheckBoxFor将生成一个没有 ID 的复选框,因此您需要在复选框中添加一个 ID,然后将您的标签指向相同的 ID。最简单的方法是使用@Html.NameFor辅助方法将复选框的名称复制到其 ID 字段中。下面是一个例子:
@Html.CheckBoxFor(x => x.Active, new {id=Html.NameFor(x => x.Active)})
<label for="@Html.NameFor(x => x.Active)">Active</label>
Generated HTML (without validation attributes):
生成的 HTML(没有验证属性):
<input id="[0].Active" name="[0].Active" type="checkbox" value="true" />
<input name="[0].Active" type="hidden" value="false" />
<label for="[0].Active">Active</label>
回答by LawrenceM
You shouldn't need the <label>tag at all:
您根本不需要<label>标签:
<div class="block mTop20">
@HtmlCheckboxFor(f => f.prop)
@Html.LabelFor(f=>f.prop,"This is the label text")
</div>
回答by stenly
I worked on something similar and got around it by this snipped code
我做了一些类似的工作,并通过这个剪下的代码解决了这个问题
<div class="row">
<div class="col-xs-3">
<label>
@Html.CheckBoxFor(x => x.AdvanceSearch.IsExactPhrase)
@Html.LabelFor(x => x.AdvanceSearch.IsExactPhrase)
</label>
</div> ...
Hope it helps the others
希望对其他人有帮助
回答by Jess
This is a really good way:
这是一个非常好的方法:
<div class="checkbox">
<label>@Html.CheckBoxFor(x => item.IsSelected) @Html.DisplayNameFor(x => item.IsSelected)</label>
</div>
- It's what is recommended by bootstrap 3.
- It works in a table.
- You can click on the check box OR the label to select the box. (This is an important usability feature.)
- The cursor is properly the "hand" instead of the arrow pointer.
- 这是bootstrap 3推荐的。
- 它在表中工作。
- 您可以单击复选框或标签来选择该框。 (这是一个重要的可用性功能。)
- 光标正确地是“手”而不是箭头指针。


