user603007
user603007

Reputation: 11804

how to create a strongly typed radiobuttonlist in asp.net mvc 3?

I am trying to create a strongly typed radiobuttonlist. I dont know how to get the list of actions out of the ModelMetaData in the HtmlExtensionClass? The list of actions is passed in through MyViewmodel in the Index action.

helperclass c#

 public static class HtmlExtensions
    {

        public static MvcHtmlString RadioButtonForList<TModel, TProperty>(
           this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression
       )
        {
            var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);

            //var actions =  metaData.ModelType; //? how can i get my list of actions in here?

            var sb = new StringBuilder();

            foreach (var action in actions)
            {
                var id = string.Format(
                    "{0}_{1}_{2}",
                    htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix,
                    metaData.PropertyName,
                    action
                );

                var radio = htmlHelper.RadioButtonFor(expression, action, new { id = id }).ToHtmlString();
                sb.AppendFormat(
                    "<label for=\"{0}\">{1}</label> {2}",
                    id,
                    HttpUtility.HtmlEncode(action),
                    radio
                );
            }
            return MvcHtmlString.Create(sb.ToString());
        }
    }

controller index action

 public ActionResult Index()
        {
            List<PageAction> actionslist = new List<PageAction>();
            actionslist.Add(new PageAction() { action = "View" });
            actionslist.Add(new PageAction() { action = "Edit" });
            actionslist.Add(new PageAction() { action = "Create" });

            return View(new MyViewModel
            {
                actions = actionslist
            });
        }


  public class PageAction
    {
        public string user { get; set; }
        public string action { get; set; }
    }

    public class MyViewModel
    {
        public List<PageAction> actions { get; set; }
    }

view

@model radioButtonlist.Models.MyViewModel
@using radioButtonlist.Models;           

@using (Html.BeginForm())
{
    @Html.RadioButtonForList(x => x.actions)
}

Upvotes: 1

Views: 543

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039498

The actions property is a list of PageAction. So you could force the helper to take as parameter a lambda expression which returns a list of this custom type, so that inside the helper you have access to the action and user properties:

public static class HtmlExtensions
{
    public static MvcHtmlString RadioButtonForList<TModel>(
        this HtmlHelper<TModel> htmlHelper, 
        Expression<Func<TModel, IEnumerable<PageAction>>> expression
    )
    {
        var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
        var actions = metaData.Model as IEnumerable<PageAction>;

        var sb = new StringBuilder();

        foreach (var pageAction in actions)
        {
            var id = string.Format(
                "{0}_{1}_{2}",
                htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix,
                metaData.PropertyName,
                pageAction.action
            );

            var radio = htmlHelper.RadioButtonFor(expression, pageAction.action, new { id = id }).ToHtmlString();
            sb.AppendFormat(
                "<label for=\"{0}\">{1}</label> {2}",
                id,
                HttpUtility.HtmlEncode(pageAction.action),
                radio
            );
        }
        return MvcHtmlString.Create(sb.ToString());
    }
}

Upvotes: 1

Related Questions