Radiobuttons instead of dropdownlist in mvc 3 application?
Asked Answered
M

2

16

I have a View where the model has a collection of items. Then I have an EditorFor template that takes care of creating a dropdownlist for the user to select one of a limited number of values for each item in the collection:

@model Consultants.Models.ProgramSkill
<tr>
    <td>@Model.Program.Name
    </td>
        <td>@Model.Program.Category
    </td>
    <td>
        @Html.DropDownListFor( model => model.Level, new SelectList(new[] { 0, 1, 2, 3, 4, 5 }, Model.Level))
    </td>
</tr>

But I would rather have radiobuttons to do the same thing, is that possible in MVC 3? If so, how?

Meany answered 25/3, 2011 at 17:8 Comment(0)
A
20

That would be a perfect candidate for a custom html helper:

using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Text;
using System.Collections.Generic;
using System.Linq.Expressions;
using System;

public static class HtmlExtensions
{
    public static MvcHtmlString RadioButtonListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> ex, IEnumerable<SelectListItem> values)
    {
        string name = ExpressionHelper.GetExpressionText(ex);
        var sb = new StringBuilder();
        int counter = 1;
        foreach (var item in values)
        {
            sb.Append(htmlHelper.RadioButtonFor(ex, item.Value, new { id = name + counter.ToString()}));
            var label = new TagBuilder("label");
            label.SetInnerText(item.Text);
            label.Attributes.Add("for", name + counter.ToString());
            sb.Append(label.ToString());
            counter++;
        }
        return MvcHtmlString.Create(sb.ToString());
    }
}

Model:

public class MyViewModel
{
    public IEnumerable<SelectListItem> Items { get; set; }
    public string Level { get; set; }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            Level = "2",
            Items = Enumerable.Range(1, 5).Select(x => new SelectListItem
            {
                Value = x.ToString(), 
                Text = "item " + x
            })
        };
        return View(model);
    }
}

and a view:

@model AppName.Models.MyViewModel

@using (Html.BeginForm())
{
    @Html.RadioButtonListFor(x => x.Level, Model.Items)
    <input type="submit" value="OK" />
}
Attest answered 25/3, 2011 at 18:4 Comment(1)
I modified the above RadioButtonFor() method to allow pre-selecting a radio button item (this is simply a matter of setting the checked attribute): sb.Append(htmlHelper.RadioButtonFor(ex, item.Value, (item.Selected ? new { Checked = "" } : null)));Fairhaired
R
8
@Html.RadioButtonFor(m => m.Level, 0)
@Html.RadioButtonFor(m => m.Level, 1)
@Html.RadioButtonFor(m => m.Level, 2)
@Html.RadioButtonFor(m => m.Level, 3)
@Html.RadioButtonFor(m => m.Level, 4)
@Html.RadioButtonFor(m => m.Level, 5)

or with a simple loop:

@for(int level = 0; level <= 5; level++)
@Html.RadioButtonFor(m => m.Level, level)
}
Receive answered 25/3, 2011 at 17:10 Comment(2)
But with this, how do I set the currently selected Level? (As I do with the dropdownlist)Meany
If Level is an int it will be selected automatically using RadioButtonForReceive

© 2022 - 2024 — McMap. All rights reserved.