2

I was wondering what the difference is between the following two ways of adding object HtmlAttributes in ASP.NET MVC 5. I am using a RadioButtonFor html helper in a foreach loop, and I was having trouble setting the name attribute for the radio buttons so that I could group them correctly. I have it working now, but I do not understand why the solution I found (solution found here:MVC RadioButtonFor group ) works and why mine does not.

Non-working method:

<td>@Html.RadioButtonFor(m => item.PollRating, 1, new { @name = Html.DisplayFor(m => item.TickerSymbol) }) Below </td>

Working method:

 <td>@Html.RadioButtonFor(m => item.PollRating, 2, new { Name = Html.DisplayFor(m => item.TickerSymbol) }) Flat </td>

The difference is using @name vs Name for the object htmlAttributes. Whenever I've had to assign a class or id while using html helpers I used the @class or @id notation, but it does not work in this scenario for name. When I hover over @name and Name in Visual Studio I see the same popup message which states:

"MvcHtmlString 'a.Name {get;}

Anonymous Types:

'a is new {MvcHtmlString Name}"

I am using Visual Studio 2015, target .NET Framework is 4.5.2, and MVC version is 5.2.3.0.

Community
  • 1
  • 1
FittyFrank
  • 124
  • 11
  • Because that's how you override the `name` attribute (see [here](http://stackoverflow.com/questions/6057865/asp-net-mvc-3-override-name-attribute-with-textboxfor) and [here](http://stackoverflow.com/questions/23912263/how-to-override-the-name-htmlattribute-of-razor)). In the other cases `@class` escapes the C# reserved "class" keyword. `@` is not necessary for `@id` just use `id`. – Jasen Jun 15 '16 at 19:02
  • Thank you for clarifying this for me. – FittyFrank Jun 15 '16 at 21:10
  • Never attempt to override the `name` attribute when using the `HtmlHelper` methods unless you want your app to fail. Your implementation makes no sense and will not correctly bind to anything. And if your doing this in a loop, then its `for (int i = 0; i < Model.Count; i++) { @Html.RadioButtonFor(m => m[i].TickerSymbol, 1) @Html.RadioButtonFor(m => m[i].TickerSymbol, 2) }` –  Jun 15 '16 at 22:36
  • Why would I not attempt to override the name attirbute? Then how could one possibly group radio buttons correctly? I am using a foreach loop as so: "@foreach (var item in Model)". How does my implementation make no sense? – FittyFrank Jun 16 '16 at 15:17
  • Because you cannot correctly bind to your model if you do. The MVC team even built protection into the code so you could not do it (but unfortunately they only took into account lower case `name`). You need to go to the MVC site an work through the tutorials and understand the basics. Your model needs a property to bind to. –  Jun 16 '16 at 23:01

1 Answers1

1

When you use @ in htm it mean that it C# code block or element , and @ in C# is you for special varibles name whene name is the same as as system comand (@ class , @interface, @int and others)

 @// line of c# code
    @{
    //c# code body
    }

Also try to use your button in this way

<td>@Html.RadioButtonFor(m => item.PollRating, 2, new { Name=item.TickerSymbol }) Flat </td>
DespeiL
  • 993
  • 9
  • 28