[RESOLVED]No overload for method 'RadioButtonFor' takes 1 arguments

I am using Radio button list to select either one options:

.cshtml:

if (Model.UserAccountSummary != null && Model.UserAccountSummary.Any())
     {
         foreach (var item in Model.UserAccountSummary)
         {
             <tr>
                 <td>
                     @Html.RadioButtonFor(modelItem => item.user_relationship_customer)@item.USER_RELATIONSHIP_OPTION1_Text

                     @Html.RadioButtonFor(modelItem => item.user_relationship_customerAndChildren)@item.USER_RELATIONSHIP_OPTION2_Text
                 </td>
             </tr>
         }
     }

How can i make this work?

Thanks

 

You need to pass the value of the radio button if it is checked. You can pass the same one as value

@Html.RadioButtonFor(modelItem => item.user_relationship_customer, item.user_relationship_customer)

PS: if you want model binding to work properly after posting, use for loop instead of foreach

The problem is simple, the extension method "RadioButtonFor", does not have
overloaded method with single argument
, where you are passing a single argument alone. That is the problem is your code. Change your code accordingly.

If you take a look at the available constructors for the RadioButtonFor() HTML Helper, you’ll see that all three of them require at least 2 parameters to be used :

RadioButtonFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>,
Object)

This constructor requires a mapping to your Model as the first parameter and a value to be stored for the Radio Button in the second. You would likely want to use something like :

@Html.RadioButtonFor(modelItem => item.user_relationship_customer, item.USER_RELATIONSHIP_OPTION1_Text)@item.USER_RELATIONSHIP_OPTION1_Text

RadioButtonFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>,
Object, IDictionary<String, Object>)

This constructor also accepts a mapping as the first parameter along with a value for the second, and adds a third parameter that will allow you to define htmlAttributes that you want to assign to the Radio Button that is rendered :

@Html.RadioButtonFor(modelItem => item.user_relationship_customer, item.USER_RELATIONSHIP_OPTION1_Text, new { @class = "yourClass" })

RadioButtonFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>,
Object, Object)

The final constructors is basically a rewritten equivalent of the one above as it accepts the same number of objects but instead uses an object for the htmlAttributes parameter instead of a Dictionary<String,Object>. The above is likely a bit easier to read.

Leave a Reply