13

我正在为一个新项目尝试 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 帮助程序没有选择检查状态。

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

编辑:我刚刚找到了 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>

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

4

7 回答 7

7

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

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

于 2009-08-04T05:47:10.940 回答
4

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

<% 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; }
}
于 2009-03-08T03:40:21.360 回答
2

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

于 2008-11-04T23:22:41.000 回答
2

我现在正在使用 vs2010,它的工作原理如下:

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

看起来更好?

于 2010-06-07T12:31:54.770 回答
1

我制作了这个 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)

奇迹般有效。如果您有桌面恐惧症,请更改生成的标记。

于 2011-01-12T17:57:16.310 回答
1

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

于 2009-02-12T20:05:24.370 回答
-1

看法:

<%=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();
}
于 2010-12-11T09:54:19.203 回答