I'm trying out asp.net mvc for a new project, and I ran across something odd. When I use the MVC UI helpers for textboxes, the values get persisted between calls. But, when I use a series of radio buttons, the checked state doesn't get persisted.
我正在为一个新项目尝试asp.net mvc,我遇到了一些奇怪的东西。当我为文本框使用MVC UI helper时,值会在调用之间持久化。但是,当我使用一系列单选按钮时,选中的状态不会被持久化。
Here's an example from my view.
这是我的一个例子。
<li>
<%=Html.RadioButton("providerType","1")%><label>Hospital</label>
<%=Html.RadioButton("providerType","2")%><label>Facility</label>
<%=Html.RadioButton("providerType","3")%><label>Physician</label>
</li>
When the form gets posted back, I build up an object with "ProviderType" as one of it's properties. The value on the object is getting set, and then I RedirectToAction with the provider as a argument. All is well, and I end up at a URL like "http://localhost/Provider/List?ProviderType=1" with ProviderType showing. The value gets persisted to the URL, but the UI helper isn't picking up the checked state.
当表单被返回时,我用“ProviderType”作为它的属性之一构建一个对象。对象上的值正在被设置,然后我将与提供程序作为参数重新定向。一切都很好,我最终访问了一个URL,如“http://localhost/Provider/List?”ProviderType = 1”ProviderType显示。值被持久化到URL,但是UI助手并没有获取已检查的状态。
I'm having this problem with listbox,
dropdownlist
, and radiobutton. Textboxes pick up the values just fine. Do you see something I'm doing wrong? I'm assuming that the helpers will do this for me, but maybe I'll just have to take care of this on my own. I'm just feeling my way through this, so your input is appreciated.
我在列表框、dropdownlist和radiobutton上有这个问题。文本框可以很好地获取这些值。你看到我做错了什么吗?我假设助手们会帮我做这个,但也许我需要自己来处理这个。我只是在摸索,所以非常感谢你的建议。
Edit: I just found the override for the SelectList constructor that takes a selected value. That took care of my dropdown issue I mentioned above.
编辑:我刚刚找到了SelectList构造函数的覆盖,该构造函数接受选定的值。这就解决了我上面提到的下拉问题。
Edit #2: I found something that works, but it pains me to do it this way. I feel like this should be inferred.
编辑#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>
Hopefully someone will come up with another way.
希望有人能想出别的办法。
7 个解决方案
#1
6
If you give the radio buttons the same name as the property on your model, then MVC will automatically set the checked attribute on the appropriate button.
如果给单选按钮的名称与模型上的属性相同,那么MVC将自动地在相应的按钮上设置已选中的属性。
I think this relies on having a strongly typed Model.
我认为这依赖于强类型模型。
#2
3
What you need is something like this in your view:
在你看来,你需要的是这样的东西:
<% foreach(var provider in (IEnumerable<Provider>)ViewData["Providers"]) { %>
<%=Html.RadioButton("ProviderType", provider.ID.ToString(), provider.IsSelected)%><label><%=provider.Name%></label>
<% } %>
And then in your controller have this:
在你的控制器中
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();
The Provider class will be something like this:
Provider类是这样的:
public class Provider
{
public int ID { get; set; }
public string Name { get; set; }
public bool IsSelected { get; set; }
}
#3
1
The form shouldn't be posting to the querystring, unless you forgot to specify the form as method="POST". How are you specifying the form? Are you using ASP.NET MVC Beta?
表单不应该发布到querystring,除非您忘记将表单指定为method="POST"。如何指定表单?你是使用ASP。净MVCβ?
#4
1
I'm using vs2010 now, it works like:
我现在使用vs2010,它的工作原理是:
<%=Html.RadioButton("ProviderType","1",Model.ProviderType==1)%><label>Hospital</label>
looks better?
看起来更好?
#5
0
Well logically it would not persist, there is no session state. Think of it as an entirely new page. In order to get your radio buttons to populate you need to persist back something like ViewData["ProviderType"] = 3 to have the radiobutton repopulate with its data.
逻辑上它不会持久,没有会话状态。把它看作一个全新的页面。为了让单选按钮填充,您需要将ViewData["ProviderType"] = 3之类的内容保存下来,以便让radiobutton重新填充它的数据。
#6
0
I've made this HTML Helper extension:
我已经做了这个HTML助手扩展:
<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
Then in the view:
然后在视图:
<%= Html.RadioButtonList("ProviderType", Model.ProviderTypeSelectList) %>
In the controller the option is mapped automagically using the standard:
在控制器中,使用标准自动映射选项:
UpdateModel(Provider)
Works like a charm. If you are tablephobic, change the markup generated.
就像一个魅力。如果您是表恐惧症患者,请更改生成的标记。
#7
-1
View:
观点:
<%=Html.RadioButton("providerType","1")%><label>Hospital</label>
<%=Html.RadioButton("providerType","2")%><label>Facility</label>
<%=Html.RadioButton("providerType","3")%><label>Physician</label>
Controller:
控制器:
public ActionResult GetType(FormCollection collection)
{
string type=collection.Get("providerType");
if(type=="1")
//code
else if(type=="2")
//code
else
//code
return View();
}
#1
6
If you give the radio buttons the same name as the property on your model, then MVC will automatically set the checked attribute on the appropriate button.
如果给单选按钮的名称与模型上的属性相同,那么MVC将自动地在相应的按钮上设置已选中的属性。
I think this relies on having a strongly typed Model.
我认为这依赖于强类型模型。
#2
3
What you need is something like this in your view:
在你看来,你需要的是这样的东西:
<% foreach(var provider in (IEnumerable<Provider>)ViewData["Providers"]) { %>
<%=Html.RadioButton("ProviderType", provider.ID.ToString(), provider.IsSelected)%><label><%=provider.Name%></label>
<% } %>
And then in your controller have this:
在你的控制器中
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();
The Provider class will be something like this:
Provider类是这样的:
public class Provider
{
public int ID { get; set; }
public string Name { get; set; }
public bool IsSelected { get; set; }
}
#3
1
The form shouldn't be posting to the querystring, unless you forgot to specify the form as method="POST". How are you specifying the form? Are you using ASP.NET MVC Beta?
表单不应该发布到querystring,除非您忘记将表单指定为method="POST"。如何指定表单?你是使用ASP。净MVCβ?
#4
1
I'm using vs2010 now, it works like:
我现在使用vs2010,它的工作原理是:
<%=Html.RadioButton("ProviderType","1",Model.ProviderType==1)%><label>Hospital</label>
looks better?
看起来更好?
#5
0
Well logically it would not persist, there is no session state. Think of it as an entirely new page. In order to get your radio buttons to populate you need to persist back something like ViewData["ProviderType"] = 3 to have the radiobutton repopulate with its data.
逻辑上它不会持久,没有会话状态。把它看作一个全新的页面。为了让单选按钮填充,您需要将ViewData["ProviderType"] = 3之类的内容保存下来,以便让radiobutton重新填充它的数据。
#6
0
I've made this HTML Helper extension:
我已经做了这个HTML助手扩展:
<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
Then in the view:
然后在视图:
<%= Html.RadioButtonList("ProviderType", Model.ProviderTypeSelectList) %>
In the controller the option is mapped automagically using the standard:
在控制器中,使用标准自动映射选项:
UpdateModel(Provider)
Works like a charm. If you are tablephobic, change the markup generated.
就像一个魅力。如果您是表恐惧症患者,请更改生成的标记。
#7
-1
View:
观点:
<%=Html.RadioButton("providerType","1")%><label>Hospital</label>
<%=Html.RadioButton("providerType","2")%><label>Facility</label>
<%=Html.RadioButton("providerType","3")%><label>Physician</label>
Controller:
控制器:
public ActionResult GetType(FormCollection collection)
{
string type=collection.Get("providerType");
if(type=="1")
//code
else if(type=="2")
//code
else
//code
return View();
}