MVC Razor单选按钮

69

在部分视图中,我使用类似这样的文本框。

@model Dictionary<string, string>
@Html.TextBox("XYZ", @Model["XYZ"])

我该如何生成单选按钮,并在表单集合中获取所需的值作为YES/NO True/False(是/否 真/假)?目前,如果我选择下面的任何值,对于“ABC”,我得到的是null。

   <label>@Html.RadioButton("ABC", @Model["ABC"])Yes</label>
   <label>@Html.RadioButton("ABC", @Model["ABC"])No</label>

控制器

        public int Create(int Id, Dictionary<string, string> formValues)
        {
         //Something Something
        }

请展示您的操作方法代码。 - Erik Funkenbusch
2
可能是ASP.NET MVC带有强绑定模型MVC的是/否单选按钮的重复问题。 - John Koerner
10个回答

73
为了对多个项目执行此操作,可以采取以下做法:
foreach (var item in Model)
{
    @Html.RadioButtonFor(m => m.item, "Yes") @:Yes
    @Html.RadioButtonFor(m => m.item, "No") @:No
}

1
我调用所有的部分视图时传递@model Dictionary<string, string>,我需要一种方法来返回单选按钮中选择的值的(True,False)或(Yes,No)。 - Nanu
1
@KirkWoll的代码无法绑定到一个Dictionary<string, string>(正如他的模型所述)。根据是/否回答,最好使用bool类型,如果允许不选择,则使用可空bool类型。 - Mathew Thompson
1
@Nikhil 好的,那就没问题了,你将会有 "Yes" 或者 "No" 作为值(如果没有选择则是 null)。试试我的更新编辑,并将你的模型保持为 Dictionary<string, string> - Mathew Thompson
@:No / @:Yes 不起作用(“:”不允许作为Razor表达式的开头)- 可能是因为我没有使用循环。 - Michael Brennt
3
"@:"被用于跳出Razor语法,类似于<text>标签,因为"Yes"和"No"基本上是HTML或非C#代码(根据你的代码结构可能不需要它)。 - Mathew Thompson
显示剩余4条评论

25

简单地说:

   <label>@Html.RadioButton("ABC", True)Yes</label>
   <label>@Html.RadioButton("ABC", False)No</label>

但是你应该始终如建议的那样使用强类型模型。


你必须小心,因为没有任何发布的解决方案执行任何服务器端验证。这里有一个优雅的解决方案,它执行客户端和服务器端验证,以确保向模型发布有效数据。https://stackoverflow.com/a/56185910/3960200 - Etienne Charland

15

我以如下方式完成了这个:

  @Html.RadioButtonFor(model => model.Gender, "M", false)@Html.Label("Male")
  @Html.RadioButtonFor(model => model.Gender, "F", false)@Html.Label("Female")

15

我用这个Stack Overflow答案解决了同样的问题。

基本上,它将单选按钮绑定到强类型模型的布尔属性。

@Html.RadioButton("blah", !Model.blah) Yes 
@Html.RadioButton("blah", Model.blah) No 
希望能帮到你!

10

MVC5 Razor Views

以下示例还将标签与单选按钮相关联(在单击相关标签时将选择单选按钮)

// 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")

这是更好的答案,因为它解决了唯一id属性的渲染问题。 - InbetweenWeekends

8
<label>@Html.RadioButton("ABC", "YES")Yes</label>
<label>@Html.RadioButton("ABC", "NO")No</label>

6

MVC Razor提供了一个优雅的Html Helper,名为RadioButton,它有两个参数(通常情况下是这样,但我们可以重载它最多五个参数),即组名称和值。

<div class="col-md-10">
    Male:   @Html.RadioButton("Gender", "Male")
    Female: @Html.RadioButton("Gender", "Female")
</div>                         

1
请解释一下你的解决方案。 - Quality Catalyst

3
<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>

1
这对我有效。
@{ var dic = new Dictionary<string, string>() { { "checked", "" } }; }
@Html.RadioButtonFor(_ => _.BoolProperty, true, (@Model.BoolProperty)? dic: null) Yes
@Html.RadioButtonFor(_ => _.BoolProperty, false, (!@Model.HomeAddress.PreferredMail)? dic: null) No

0

我想分享一种不使用@Html.RadioButtonFor辅助程序来完成单选按钮(以及整个HTML表单)的方法,尽管我认为@Html.RadioButtonFor可能是更好、更新的方式(首先,它是强类型的,因此与ModelProperty密切相关)。尽管如此,这里有一种老式的、不同的方法可以实现它:

    <form asp-action="myActionMethod" method="post">
        <h3>Do you like pizza?</h3>
        <div class="checkbox">
            <label>
                <input asp-for="likesPizza"/> Yes
            </label>
        </div>
    </form>

这段代码可以放在 myView.cshtml 文件中,还使用类来获取单选按钮(复选框)的格式。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接