【问题标题】:MVC Razor Radio ButtonMVC 剃刀单选按钮
【发布时间】:2012-06-04 01:14:28
【问题描述】:

局部视图 我使用这样的文本框。

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

我怎样才能生成单选按钮,并在表单集合中获得所需的值作为 YES/NO True/False)?目前,如果我为以下选择任何值,“ABC”的值为空。

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

【问题讨论】:

标签: c# .net asp.net-mvc asp.net-mvc-3 razor


【解决方案1】:

为了对多个项目执行此操作,请执行以下操作:

foreach (var item in Model)
{
    @Html.RadioButtonFor(m => m.item, "Yes") @:Yes
    @Html.RadioButtonFor(m => m.item, "No") @:No
}

【讨论】:

  • 我通过@model Dictionary 调用我所有的部分视图,我需要一种方法来为单选按钮中的选定值返回 (True, False) 或 (Yes,No)。
  • @KirkWoll 他的代码不会绑定到Dictionary&lt;string, string&gt;(如他的模型所述)。基于 Yes No 响应,一个 bool 可能是最好的,如果您不允许选择任何一个,则为 Nullable bool。
  • @KirkWoll 检查我更新的编辑,我原本以为他只想要一个选项是 Yes No,因此推荐一个 bool。
  • @Nikhil 嗯,这很好,然后您将使用“是”或“否”作为值(或者 null,如果没有选择任何内容)。试试我更新的编辑,并保持你的模型为Dictionary&lt;string, string&gt;
  • @MichaelBrennt "@:" 用于突破 Razor,类似于 &lt;text&gt; 标签,因为 Yes 和 No 基本上是 HTML/非 C# 代码(您可能不需要它,具体取决于你的结构)。
【解决方案2】:

简单地说:

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

但您应该始终使用 cacho 建议的强类型模型。

【讨论】:

  • 您必须小心,因为已发布的解决方案都不会执行任何类型的服务器端验证。这是一个优雅的解决方案,它执行客户端和服务器端验证,以确保将有效数据发布到模型。 stackoverflow.com/a/56185910/3960200
【解决方案3】:

我这样做的方式如下:

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

【讨论】:

    【解决方案4】:

    我用this SO answer解决了同样的问题。

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

    @Html.RadioButton("blah", !Model.blah) Yes 
    @Html.RadioButton("blah", Model.blah) No 
    

    希望对你有帮助!

    【讨论】:

      【解决方案5】:
      <label>@Html.RadioButton("ABC", "YES")Yes</label>
      <label>@Html.RadioButton("ABC", "NO")No</label>
      

      【讨论】:

        【解决方案6】:

        MVC5剃刀视图

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

        // 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 属性的呈现问题。
        【解决方案7】:

        MVC Razor 提供了一个优雅的 Html Helper,称为 RadioButton,它有两个参数(这是通用的,但我们可以重载它最多五个参数),一个是组名,另一个是值

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

        【讨论】:

        • 请解释您的解决方案。
        【解决方案8】:
        <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>
        

        【讨论】:

          【解决方案9】:

          这对我有用。

          @{ 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
          

          【讨论】:

            【解决方案10】:

            我想分享一种在不使用 @Html.RadioButtonFor 帮助器的情况下执行单选按钮(和整个 HTML 表单)的方法,尽管我认为 @Html.RadioButtonFor 可能是更好和更新的方法(一方面,它非常强大类型,因此与模型属性密切相关)。不过,这里有一种老式的、不同的方法:

                <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 文件中,也可以使用类来获取单选按钮(复选框)格式。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2020-08-27
              • 2013-08-05
              • 1970-01-01
              • 2012-06-30
              • 2013-11-08
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多