Nothing
Nothing

Reputation: 2642

Html.RadioButtonFor for asp.net mvc

I want to survey about the service of my product.

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

The data are taken from database, and each option use as radio button. I loop data in my views of my asp.net project :

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

  <%
   }
  %>

But the problem is I do this all my radio button has the same name.

I tried in foreach block :

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

Then got an error : Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

Upvotes: 1

Views: 1041

Answers (1)

webdeveloper
webdeveloper

Reputation: 17288

Look at HtmlHelper.RadioButton Method (String, Object)

public IHtmlString RadioButton(
    string name,
    Object value
)

You will get something like this:

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

Upvotes: 1

Related Questions