Boris Callens
Boris Callens

Reputation: 93307

Why doesn't my first radiobutton get checked?

I got the following Html:

<div class="horizontalRadio">
    <label for="SearchBag.DisplayTypeChips" id="DisplayTypeChipsLabel">
        <%=ViewData.Model.T9nProvider.TranslateById("CommonWeb.Chips")%>
    </label>
    <%=Html.RadioButton("displayType", DisplayTypes.Chip,
        Model.DisplayType.Equals(DisplayTypes.Chip.ToString(), StringComparison.InvariantCultureIgnoreCase),
        new { @id = "SearchBag.DisplayTypeChips" })%>
</div>
<div class="horizontalRadio">
    <label for="SearchBag.DisplayTypeGrid" id="DisplayTypeGridLabel">
        <%=ViewData.Model.T9nProvider.TranslateById("CommonWeb.Grid")%>
    </label>
    <%=Html.RadioButton("displayType", DisplayTypes.Grid,
       Model.DisplayType.Equals(DisplayTypes.Grid.ToString(), StringComparison.InvariantCultureIgnoreCase),
        new { @id = "SearchBag.DisplayTypeGrid" })%>
</div>

Whenever Model.DisplayType is "grid", everything is fine; the second button is checked.
When the value is "chip", nothing is checked. In the debugger, I can see that Model.DisplayType.Equals(DisplayTypes.Chip.ToString()) is true. When I change the name of the buttons to something different, it works too.
Happens I don't want to change the name because this is the name that makes sense. This is the name I'm using throughout my application ...

Any ideas WHY this name is evil?

Upvotes: 1

Views: 202

Answers (1)

joshcomley
joshcomley

Reputation: 28808

There is an issue in ASP.NET MVC with regards to having a property on your model with the same name as the name of a "grouped" element (like a select or a radio group).

See http://weblogs.asp.net/ashicmahtab/archive/2009/03/27/asp-net-mvc-html-dropdownlist-and-selected-value.aspx for details.

It might be this, it's worth trying.

Upvotes: 2

Related Questions