asp.net mvc的单选按钮状态 [英] asp.net mvc radio button state

查看:509
本文介绍了asp.net mvc的单选按钮状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了asp.net的MVC一个新的项目,我碰到一些奇怪的跑了。当我使用文本框的MVC UI辅助,价值观得到调用之间持续存在。但是,当我使用了一系列的单选按钮,选中状态没有得到持续。

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.

下面是从我的观点的例子。

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>

当的形式被发布后,我建立了一个对象ProviderType,因为它的属性之一。在对象上的价值是越来越设置,然后我与供应商作为一个参数RedirectToAction。一切都很好,我喜欢在HTTP?://本地主机/供应商/列表ProviderType = 1的URL结了ProviderType表现。该值将被持久化到URL,但用户界面佣工不拿起选中状态。

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.

我有这个问题,列表框,<罢工>的DropDownList 和单选按钮。文本框拿起值就好了。你看到的东西我做错了吗?我假设佣工会为我做到这一点,但也许我就必须照顾这对我自己的。我只是觉得通过这次我的方式,让你的输入AP preciated。

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.

编辑:我刚刚发现的选择列表的构造函数选定值覆盖。这花了我我上面提到的下拉问题的照顾。

I just found the override for the SelectList constructor that takes a selected value. That took care of my dropdown issue I mentioned above.

编辑#2:我发现了一些作品,但它的痛苦,我做这种方式。我觉得这应该是推断。

Edit #2: I found something that works, but it pains me to do it this way. I feel like this should be inferred.

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

推荐答案

如果你给单选按钮相同的名称作为模型的属性,那么MVC会自动设置上的相应按钮选中属性。

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.

我觉得这依赖于拥有一个强类型的模型。

I think this relies on having a strongly typed Model.

这篇关于asp.net mvc的单选按钮状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆