views:

297

answers:

2

Again a question about RadioButtons/ RadioButtonList

I've the following Model:

public class SkillLevelModel
    {
        public long? Id { get; set; }
        public int? SelectedLevel { get; set;}
    }

I've the following Controller:

public class SkillController : BaseController
    {
        public ActionResult Index()
        {
            var skills = new List<SkillLevelModel>();

            for (int i = 0; i < 10; i++)
                skills.Add(new SkillLevelModel() { Id = i, SelectedLevel = new Random().Next(0,5) });

            return View(skills);
        }

I've the following code in the View:

<% foreach (var item in Model) { %>
    <tr>
        <td style="width: 30px" align="center">
            <%= Html.Encode(item.Id) %>
        </td>
        <td>
            <% Html.RenderPartial("UCSkillLevel", item); %>
        </td>
    </tr>
    <% } %>

I've the following code in the PartialView:

<% for (int i = 0; i <= 5; i++) { %>
    <td align="center">
        <%= Html.RadioButton("SelectedLevel", i, new { id = Model.Id + "_" + i })%>
    </td>
<% } %>

The problem is that no radiobutton is checked, althought they have a level.

What's wrong here?

A: 

You have to specify if the radiobutton is selected:

Html.RadioButton("SelectedLevel", i, i == (Model.SelectedLevel ?? -1), new { id = Model.Id + "_" + i })
Cristi Todoran
+1  A: 

Code in the partial view foreach loop should be

<%= Html.RadioButton("SelectedLevel_" + Model.Id, i, Model.SelectedLevel == i) %>
Stef