访问 IEnumerable mvc2 单选按钮的值

发布于 2024-12-21 01:31:25 字数 1564 浏览 1 评论 0原文

我有一个是/否单选按钮,需要访问另一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

め七分饶幸 2024-12-28 01:31:25

我认为问题在于可枚举永远不会存储选定的值,这只是在一定程度上丢失了。

因此,您需要两部分,一个用于存储值,另一个用于包含要显示的单选按钮。

[DisplayName("Are you registered as blind (severely sight impaired)?")]
public int RegBlind { get; set; }

public IEnumerable<RadioButtonViewModel> RegBlindOptions { get; set; }

完成此排序后,您应该能够将 EditorTemplate 用作部分视图(不过,这可能意味着将其从 EditorTemplates 目录移出并移至相应的 Views 文件夹或共享文件夹中)。

<% Html.RenderPartial("YesNoEditorTemplate", Model.RegBlindOptions)%>

那应该有效或接近。如果没有,请查看生成的 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.

[DisplayName("Are you registered as blind (severely sight impaired)?")]
public int RegBlind { get; set; }

public IEnumerable<RadioButtonViewModel> RegBlindOptions { get; set; }

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

<% Html.RenderPartial("YesNoEditorTemplate", Model.RegBlindOptions)%>

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文