【问题标题】:asp.net mvc radio button stateasp.net mvc 单选按钮状态
【发布时间】:2008-11-04 20:38:11
【问题描述】:

我正在为一个新项目尝试 asp.net mvc,但遇到了一些奇怪的事情。当我将 MVC UI 助手用于文本框时,值会在调用之间保持不变。但是,当我使用一系列单选按钮时,选中状态不会持续存在。

这是我认为的一个例子。

<li>
        <%=Html.RadioButton("providerType","1")%><label>Hospital</label>
        <%=Html.RadioButton("providerType","2")%><label>Facility</label>
        <%=Html.RadioButton("providerType","3")%><label>Physician</label>
</li>

当表单被回发时,我构建了一个对象,其中“ProviderType”作为它的属性之一。对象上的值被设置,然后我 RedirectToAction 以提供者作为参数。一切都很好,我最终得到了一个像“http://localhost/Provider/List?ProviderType=1”这样的 URL,其中显示了 ProviderType。该值被持久化到 URL,但 UI 帮助程序没有选择检查状态。

我在使用列表框、dropdownlist 和单选按钮时遇到了这个问题。文本框可以很好地获取值。你看到我做错了什么吗?我假设助手会为我做这件事,但也许我只需要自己处理这件事。我只是在摸索,所以感谢您的意见。

编辑:我刚刚找到了 SelectList 构造函数的覆盖,该构造函数采用选定的值。这解决了我上面提到的下拉问题。

编辑#2:我发现了一些可行的方法,但这样做让我很痛苦。我觉得应该这样推断。

<li>
  <%=Html.RadioButton("ProviderType","1",Request["ProviderType"]=="1")%><label>Hospital</label>
  <%=Html.RadioButton("ProviderType", "2", Request["ProviderType"] == "2")%><label>Facility</label>
  <%=Html.RadioButton("ProviderType", "3", Request["ProviderType"] == "3")%><label>Physician</label>
</li>

希望有人会想出另一种方法。

【问题讨论】:

  • 自正式发布以来,我还没有看到任何类似的问题。这可能与测试版有关,或者我做错了什么。无论如何,我认为这个问题不再相关。

标签: asp.net-mvc ui-helper


【解决方案1】:

如果您为单选按钮指定与模型上的属性相同的名称,则 MVC 将自动在相应按钮上设置选中的属性。

我认为这依赖于强类型模型。

【讨论】:

    【解决方案2】:

    在你看来,你需要的是这样的东西:

    <% foreach(var provider in (IEnumerable<Provider>)ViewData["Providers"]) { %>
        <%=Html.RadioButton("ProviderType", provider.ID.ToString(), provider.IsSelected)%><label><%=provider.Name%></label>
    <% } %>
    

    然后在你的控制器中有这个:

    var providers = GetProviders();
    int selectedId = (int) Request["ProviderType"]; // TODO: Use Int32.TryParse() instead
    foreach(var p in providers)
    {
        if (p.ID == selectedId)
        {
            p.IsSelected = true;
            break;
        }
    }
    ViewData["Providers"] = providers;
    return View();
    

    Provider 类将是这样的:

    public class Provider
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public bool IsSelected { get; set; }
    }
    

    【讨论】:

      【解决方案3】:

      该表单不应发布到查询字符串,除非您忘记将表单指定为 method="POST"。你是如何指定表格的?您使用的是 ASP.NET MVC Beta 吗?

      【讨论】:

        【解决方案4】:

        我现在用的是vs2010,效果如下:

        <%=Html.RadioButton("ProviderType","1",Model.ProviderType==1)%><label>Hospital</label> 
        

        看起来更好?

        【讨论】:

          【解决方案5】:

          从逻辑上讲,它不会持续存在,没有会话状态。把它想象成一个全新的页面。为了让您的单选按钮被填充,您需要保留类似 ViewData["ProviderType"] = 3 的内容,以使单选按钮重新填充其数据。

          【讨论】:

            【解决方案6】:

            我已经制作了这个 HTML Helper 扩展:

                <Extension()> _
                Public Function RadioButtonList(ByVal helper As HtmlHelper, ByVal name As String, ByVal Items As IEnumerable(Of String)) As String
                    Dim selectList = New SelectList(Items)
                    Return helper.RadioButtonList(name, selectList)
                End Function
            
                <Extension()> _
                Public Function RadioButtonList(ByVal helper As HtmlHelper, ByVal Name As String, ByVal Items As IEnumerable(Of SelectListItem)) As String
                    Dim sb As New StringBuilder
                    sb.Append("<table class=""radiobuttonlist"">")
                    For Each item In Items
                        sb.AppendFormat("<tr><td><input id=""{0}_{1}"" name=""{0}"" type=""radio"" value=""{1}"" {2} /><label for=""{0}_{1}"" id=""{0}_{1}_Label"">{3}</label></td><tr>", Name, item.Value, If(item.Selected, "selected", ""), item.Text)
                    Next
                    sb.Append("</table>")
                    Return sb.ToString()
                End Function
            

            然后在视图中:

            <%= Html.RadioButtonList("ProviderType", Model.ProviderTypeSelectList) %>
            

            在控制器中,选项使用标准自动映射:

            UpdateModel(Provider)
            

            像魅力一样工作。如果您有桌面恐惧症,请更改生成的标记。

            【讨论】:

              【解决方案7】:

              查看:

              <%=Html.RadioButton("providerType","1")%><label>Hospital</label>
              <%=Html.RadioButton("providerType","2")%><label>Facility</label>
              <%=Html.RadioButton("providerType","3")%><label>Physician</label>
              

              控制器:

              public ActionResult GetType(FormCollection collection)
              {
               string type=collection.Get("providerType");
              
                if(type=="1")
                 //code
                else if(type=="2")
                 //code
                else
                 //code
              
               return View();
              }
              

              【讨论】:

                猜你喜欢
                • 2011-02-15
                • 1970-01-01
                • 2015-03-19
                • 1970-01-01
                • 2013-10-05
                • 1970-01-01
                • 1970-01-01
                • 2016-04-16
                • 1970-01-01
                相关资源
                最近更新 更多