C# MVC Razor 单选按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10805502/
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
MVC Razor Radio Button
提问by Nanu
In partial viewI work with textboxes like this.
在局部视图中,我使用这样的文本框。
@model Dictionary<string, string>
@Html.TextBox("XYZ", @Model["XYZ"])
How can i generate radiobuttons, and get the desired value in the form collection as YES/NO True/False) ? Currently i am getting null for "ABC" if i select any value for the below.
我如何生成单选按钮,并在表单集合中获取所需的值作为 YES/NO True/False) ?目前,如果我为以下选择任何值,我将获得“ABC”的空值。
<label>@Html.RadioButton("ABC", @Model["ABC"])Yes</label>
<label>@Html.RadioButton("ABC", @Model["ABC"])No</label>
Controller
控制器
public int Create(int Id, Dictionary<string, string> formValues)
{
//Something Something
}
采纳答案by mattytommo
In order to do this for multiple items do something like:
为了对多个项目执行此操作,请执行以下操作:
foreach (var item in Model)
{
@Html.RadioButtonFor(m => m.item, "Yes") @:Yes
@Html.RadioButtonFor(m => m.item, "No") @:No
}
回答by Cacho Santa
回答by Nanu
<label>@Html.RadioButton("ABC", "YES")Yes</label>
<label>@Html.RadioButton("ABC", "NO")No</label>
回答by Kuqd
Simply :
简单地 :
<label>@Html.RadioButton("ABC", True)Yes</label>
<label>@Html.RadioButton("ABC", False)No</label>
But you should always use strongly typed model as suggested by cacho.
但是您应该始终按照 cacho 的建议使用强类型模型。
回答by jayson.centeno
This works for me.
这对我有用。
@{ var dic = new Dictionary<string, string>() { { "checked", "" } }; }
@Html.RadioButtonFor(_ => _.BoolProperty, true, (@Model.BoolProperty)? dic: null) Yes
@Html.RadioButtonFor(_ => _.BoolProperty, false, ([email protected])? dic: null) No
回答by Anjan Kant
I done this in a way like:
我以如下方式完成此操作:
@Html.RadioButtonFor(model => model.Gender, "M", false)@Html.Label("Male")
@Html.RadioButtonFor(model => model.Gender, "F", false)@Html.Label("Female")
回答by Buhlebesizwe
<p>@Html.RadioButtonFor(x => x.type, "Item1")Item1</p>
<p>@Html.RadioButtonFor(x => x.type, "Item2")Item2</p>
<p>@Html.RadioButtonFor(x => x.type, "Item3")Item3</p>
回答by Debendra Dash
MVC Razor provides one elegant Html Helper called RadioButtonwith two parameters (this is general, But we can overload it uptil five parameters) i.e. one with the group name and other being the value
MVC Razor 提供了一个优雅的 Html Helper,称为RadioButton,有两个参数(这是通用的,但我们可以重载它直到五个参数),即一个是组名,另一个是值
<div class="col-md-10">
Male: @Html.RadioButton("Gender", "Male")
Female: @Html.RadioButton("Gender", "Female")
</div>
回答by Prasad De Silva
MVC5Razor Views
MVC5剃刀视图
Below example will also associate labels with radio buttons (radio button will be selected upon clicking on the relevant label)
下面的示例还将标签与单选按钮相关联(单击相关标签时将选择单选按钮)
// replace "Yes", "No" --> with, true, false if needed
@Html.RadioButtonFor(m => m.Compatible, "Yes", new { id = "compatible" })
@Html.Label("compatible", "Compatible")
@Html.RadioButtonFor(m => m.Compatible, "No", new { id = "notcompatible" })
@Html.Label("notcompatible", "Not Compatible")
回答by MS Wani
<table>
<tr>
<td align="right" style="height:26px;">Is Calender Required?:</td>
<td align="left">
@Html.RadioButton("rdbCalenderRequested", "True", new { id = "rdbCalenderRequested_1" })@:Yes
@Html.RadioButton("rdbCalenderRequested", "False", new { id = "rdbCalenderRequested_2" }) @:No
</td>
<td align="right" style="height:26px;">Is Special Pooja?:</td>
<td align="left">
@Html.RadioButton("rdbPoojaRequested", "True", new { id = "rdbPoojaRequested_1" })@:Yes
@Html.RadioButton("rdbPoojaRequested", "False", new { id = "rdbPoojaRequested_2" }) @:No
</td>
</tr>
</table>

