简体   繁体   English

RadioButtonFor和标签

[英]RadioButtonFor and label

I'm using RadioButtonFor in my web project, but I want to use the label in order to select the item if the user selects the text of the radio button, but the html generated gives me multiple same #id's because of the Radiobuton and I want to change this in order to have clear html and use jquery selectors without troubles (with the unique id): 我在Web项目中使用RadioButtonFor ,但是如果用户选择单选按钮的文本,我想使用标签来选择项目,但是由于Radiobuton和我,生成的html给了我多个相同的#id想要更改此内容以使HTML清晰并使用jquery选择器而没有麻烦(具有唯一ID):

CSHTML 网页HTML

@foreach (var item in Model.audit.natureSelect)
{
    <label>
        @Html.RadioButtonFor(m => m.audit.nature, item.Id, new { Value = item.Id }) @item.NatAudit
    </label>
}

HTML 的HTML

<div class="col-md-10">
    <label><input id="audit_type" name="audit.type" type="radio" value="1">item 1</label> 
    <label><input id="audit_type" name="audit.type" type="radio" value="2">item 2</label> 
    <label><input id="audit_type" name="audit.type" type="radio"value="3">item 3</label>
</div>

Thanks to help me to make it clean 谢谢帮我把它弄干净

You are not using the RadioButtonFor helper correctly. 您没有正确使用RadioButtonFor助手。 What you need is for all the radio buttons to have a single name, but different ids. 您需要的是所有单选按钮都具有单个名称,但ID不同。 And, here's how you do it: 并且,这是您的操作方式:

@foreach (var item in Model.audit.natureSelect)
{
    <label>
        @Html.RadioButtonFor(m => m.audit.nature, item.Id, new { id = item.Id }) @item.NatAudit
    </label>
}

By the way, follow C# naming conventions when naming your properties and methods. 顺便说一句,在命名属性和方法时,请遵循C#命名约定。 Property names in C# should start with a capital letter. C#中的属性名称应以大写字母开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM