访问 IEnumerable mvc2 单选按钮的值
我有一个是/否单选按钮,需要访问另一个 aspx 表单上的值。我不知道我这样做对不对?
主视图模型
[UIHint("YesNoEditorTemplate")]
[DisplayName("Are you registered as blind (severely sight impaired)?")]
public IEnumerable<RadioButtonViewModel> RegBlind { get; set; }
单选按钮视图模型
public class RadioButtonViewModel
{
public string ID { get; set; }
public string Name { get; set; }
public int Value { get; set; }
public string Text { get; set; }
}
控制器
List<RadioButtonViewModel> regBlindList = new List<RadioButtonViewModel>();
regBlindList.Add(CreateRadioButton("RegBlind", 1, "Yes"));
regBlindList.Add(CreateRadioButton("RegBlind", 0, "No"));
badgeViewModel.RegBlind = regBlindList;
编辑器模板
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Tameside.Internal.ViewModels.BlueBadge.RadioButtonViewModel>>"%><%
foreach (var model in Model)
{%>
<tr>
<td><%=String.Format("<input type=\"radio\" id=\"{0}\" name=\"{1}\" value=\"{2}\" />", model.ID, model.Name,model.Value)%></td>
<td><%=String.Format("<label for=\"{0}\">{1}</label>", model.ID, model.Text)%></td>
</tr><%
}%>
ASPX 页面
<%=Html.EditorFor(x => x.RegBlind)%>
这是我访问信息的方式
if (Model.RegBlind.First().Value == 0)
这是正确的吗?
预先感谢您的任何帮助。
克莱尔
I have a yes/no radio button and need to access the value on another aspx form. I don't know whether I am doing this right or not?
Main viewmodel
[UIHint("YesNoEditorTemplate")]
[DisplayName("Are you registered as blind (severely sight impaired)?")]
public IEnumerable<RadioButtonViewModel> RegBlind { get; set; }
Radio button viewmodel
public class RadioButtonViewModel
{
public string ID { get; set; }
public string Name { get; set; }
public int Value { get; set; }
public string Text { get; set; }
}
Controller
List<RadioButtonViewModel> regBlindList = new List<RadioButtonViewModel>();
regBlindList.Add(CreateRadioButton("RegBlind", 1, "Yes"));
regBlindList.Add(CreateRadioButton("RegBlind", 0, "No"));
badgeViewModel.RegBlind = regBlindList;
Editor Template
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Tameside.Internal.ViewModels.BlueBadge.RadioButtonViewModel>>"%><%
foreach (var model in Model)
{%>
<tr>
<td><%=String.Format("<input type=\"radio\" id=\"{0}\" name=\"{1}\" value=\"{2}\" />", model.ID, model.Name,model.Value)%></td>
<td><%=String.Format("<label for=\"{0}\">{1}</label>", model.ID, model.Text)%></td>
</tr><%
}%>
ASPX page
<%=Html.EditorFor(x => x.RegBlind)%>
Here is how I am accessing the information
if (Model.RegBlind.First().Value == 0)
Is this correct?
Thanks in advance for any help.
Clare
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题在于可枚举永远不会存储选定的值,这只是在一定程度上丢失了。
因此,您需要两部分,一个用于存储值,另一个用于包含要显示的单选按钮。
完成此排序后,您应该能够将 EditorTemplate 用作部分视图(不过,这可能意味着将其从 EditorTemplates 目录移出并移至相应的 Views 文件夹或共享文件夹中)。
那应该有效或接近。如果没有,请查看生成的 HTML 并检查一切是否正常,然后查看 POST 标头中发回的内容:)
I think the issue is that the enumerable will never store the selected value, that's just being lost to a degree.
So you need two parts, one to store the value and another to contain the radio buttons you want to display.
Once you've got this sorted, you should be able to use your EditorTemplate as a partial view (though, that'd probably mean moving it out of the EditorTemplates directory and into the appropriate Views folder or shared).
That should work or be close. If not, have a look at the HTML generated and check everything is there ok, then have a nosey at what is sent back in the POST headers :)