C# 如何将标签与单选按钮关联?

C# 如何将标签与单选按钮关联?,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,我正在使用Html.radiobutton for,想知道使用它的最佳实践是什么。例如,下面是两种方法,选项1给出了我想要的结果。我只是在我的模型中声明DeliveryFeeType,并在上面使用Label 备选方案-1: .ASPX 输出: 现在,我想在模型中声明属性的地方使用标签。我不喜欢的是,All和FlatFee在调试时总是以false出现。看起来我只是用它们在UI上显示标签。根据单选按钮的选择,我可以将它们设置为真或假?如果是,我怎么做 备选方案2: .ASPX 输出: 要关联元素,

我正在使用
Html.radiobutton for
,想知道使用它的最佳实践是什么。例如,下面是两种方法,选项1给出了我想要的结果。我只是在我的模型中声明
DeliveryFeeType
,并在上面使用
Label

备选方案-1:

.ASPX

输出:

现在,我想在
模型
中声明属性的地方使用
标签。我不喜欢的是,
All
FlatFee
在调试时总是以false出现。看起来我只是用它们在UI上显示标签。根据
单选按钮
的选择,我可以将它们设置为
?如果是,我怎么做

备选方案2:

.ASPX

输出:

要关联
元素,可以将表单控件包装在
中,也可以使用
for
属性将其与控件的
id
属性关联

radiobutton()
方法的问题在于,它会生成重复的
id
属性,这些属性是无效的html,因此您应该将其删除(如果用
包装)或显式设置它

选择1

<label>
    <%= Html.RadioButtonFor(m => m.DeliveryFeeType, "All", new { id = "" }) %>
    <span>All</span>
</label>
<label>
    <%= Html.RadioButtonFor(m => m.DeliveryFeeType, "Flat Fee", new { id = "" }) %>
    <span>Flat Fee</span>
</label>

m、 DeliveryFeeType,“全部”,新的{id=”“})%>
全部的
m、 DeliveryFeeType,“固定费用”,新的{id=”“})%>
固定费用
选择2

<%= Html.RadioButtonFor(m => m.DeliveryFeeType, "All", new { id = "all" }) %>
<%= Html.Label("All", new { @for = "all" }) %>
<%= Html.RadioButtonFor(m => m.DeliveryFeeType, "All", new { id = "flatfee" }) %>
<%= Html.Label("Flat Fee", new { @for = "flatfee" }) %>
m.DeliveryFeeType,“All”,新的{id=“All”})%>
m、 DeliveryFeeType,“全部”,新的{id=“flatfee”})%>

您需要执行以下操作:

<fieldset>
    <legend>@Html.DisplayNameFor( m => m.Foo )</legend>

    <label>@Html.RadioButtonFor( m => m.Foo, "Value1" ) Value One</label>
    <label>@Html.RadioButtonFor( m => m.Foo, "Value2" ) Value Two</label>
    <label>@Html.RadioButtonFor( m => m.Foo, "Value3" ) Value Three</label>
</fieldset>

@DisplayNameFor(m=>m.Foo)
@RadioButton(m=>m.Foo,“Value1”)值一
@RadioButton(m=>m.Foo,“Value2”)值2
@RadioButton(m=>m.Foo,“Value3”)值三
或者这个:

<fieldset>
    <legend>@Html.DisplayNameFor( m => m.Foo )</legend>

    <label for="radio1">Value One</label>
    @html.RadioButtonFor( m => m.Foo, "Value1", new { id="radio1" } )

    <label for="radio2">Value Two</label>
    @html.RadioButtonFor( m => m.Foo, "Value2", new { id="radio2" } )

    <label for="radio3">Value Three</label>
    @html.RadioButtonFor( m => m.Foo, "Value3", new { id="radio3" } )

</fieldset>

@DisplayNameFor(m=>m.Foo)
价值一
@RadioButton(m=>m.Foo,“Value1”,新的{id=“radio1”})
价值二
@html.RadioButtonFor(m=>m.Foo,“Value2”,新的{id=“radio2”})
价值三
@RadioButton(m=>m.Foo,“Value3”,新的{id=“radio3”})
这是因为单个ViewModel属性对应于值的持有者,而不是单个属性值本身-请记住,在HTML中,单选按钮为同一表单键/值对显示多个
元素(而复选框为每个键/值对显示一个输入)


另一个问题是MVC生成的
id=”“
属性值不正确:它将生成重复的属性值,因为它完全基于属性名称,所以您需要手动覆盖
id=“
属性(因此实际上,我给出的第一个示例将由于重复
id=“”而给您无效的HTML标记)
属性值)。

您没有为
All
FlatFee
生成任何表单控件,因此不会发布任何值,因此它们是默认值(
false
)。但是为什么你认为你需要这些属性呢?@StephenMuecke在同一页中,我看到了几个遵循这种模式的属性。像这样,
m.EnableDelivery)%>m.EnableDelivery)%>[DisplayName(“为本店启用配送”)]public bool EnableDelivery{get;set;}
所以我想知道,我能不能用
RadioButton
做类似的事情。你的问题是如何将标签与单选按钮关联?@StephenMuecke我想是的。如果标题有误导性,我可以更改标题。好的,我会添加一个答案,但我也会编辑这个问题来解释你想要什么(或者你可以做什么)-你想做什么还不是很清楚。使用这些选项我不会得到结果。选项2抛出一个错误
},预期为
。对我来说很好,但是有错误。即使我选中任何单选按钮,
All
FlatFee
仍然为false。哪一行出错?你说没有结果是什么意思?您需要从您的模型中删除2个
bool
属性-您对属性的绑定
DeliveryFeeType
(不是
bool All
bool FlatFee
-它们不是必需的,也没有意义包含在模型中)选项2中
Html.Label附近的错误。如果我把这两个属性取出来,那么我就可以从我的问题中用选项1得到答案。哎呀,对于
(至少在razor中是这样-但我记得这是否也适用于
aspx
引擎)
<label>
    <%= Html.RadioButtonFor(m => m.DeliveryFeeType, "All", new { id = "" }) %>
    <span>All</span>
</label>
<label>
    <%= Html.RadioButtonFor(m => m.DeliveryFeeType, "Flat Fee", new { id = "" }) %>
    <span>Flat Fee</span>
</label>
<%= Html.RadioButtonFor(m => m.DeliveryFeeType, "All", new { id = "all" }) %>
<%= Html.Label("All", new { @for = "all" }) %>
<%= Html.RadioButtonFor(m => m.DeliveryFeeType, "All", new { id = "flatfee" }) %>
<%= Html.Label("Flat Fee", new { @for = "flatfee" }) %>
<fieldset>
    <legend>@Html.DisplayNameFor( m => m.Foo )</legend>

    <label>@Html.RadioButtonFor( m => m.Foo, "Value1" ) Value One</label>
    <label>@Html.RadioButtonFor( m => m.Foo, "Value2" ) Value Two</label>
    <label>@Html.RadioButtonFor( m => m.Foo, "Value3" ) Value Three</label>
</fieldset>
<fieldset>
    <legend>@Html.DisplayNameFor( m => m.Foo )</legend>

    <label for="radio1">Value One</label>
    @html.RadioButtonFor( m => m.Foo, "Value1", new { id="radio1" } )

    <label for="radio2">Value Two</label>
    @html.RadioButtonFor( m => m.Foo, "Value2", new { id="radio2" } )

    <label for="radio3">Value Three</label>
    @html.RadioButtonFor( m => m.Foo, "Value3", new { id="radio3" } )

</fieldset>