用于asp.net mvc的Html.RadioButtonFor

时间:2013-02-21 10:52:16

标签: asp.net-mvc

我想调查一下我的产品服务。

1   Was the product or service provided in a timely manner? 
    - Very Satisfied
    - Satisfied
    - Neutral
2   Was the product or service complete and accurate? 
    - Very Satisfied
    - Satisfied
    - Neutral

数据来自数据库,每个选项都用作单选按钮。我在我的asp.net项目视图中循环数据:

  <%      
   foreach (var serviceQ in _question)
   {
  %>
      <%:Html.RadioButtonFor(model => model.Option, serviceQ.Option1)%>
      <%:Html.LabelFor(m => m.Option1) %>
      <%:serviceQ.Option1 %>
      <%:Html.ValidationMessageFor(model =>model.Option1) %>

      <%:Html.RadioButtonFor(model => model.Option, serviceQ.Option2)%>
      <%:Html.LabelFor(m => m.Option2) %>
      <%:serviceQ.Option2 %>
      <%:Html.ValidationMessageFor(model =>model.Option2) %>

  <%
   }
  %>

但问题是我这样做我的所有单选按钮都有相同的名称。

我试过foreach阻止:

<%:Html.RadioButtonFor(model => model.Option + serviceQ.ID, serviceQ.Option1)%>
<%:Html.RadioButtonFor(model => model.Option + serviceQ.ID, serviceQ.Option2)%>

然后收到错误:Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

1 个答案:

答案 0 :(得分:1)

查看HtmlHelper.RadioButton Method (String, Object)

public IHtmlString RadioButton(
    string name,
    Object value
)

你会得到这样的东西:

<%:Html.RadioButton(model.Option + serviceQ.ID, serviceQ.Option1)%>