1

我正在尝试创建一个强类型的单选按钮列表。我不知道如何从 HtmlExtensionClass 中的 ModelMetaData 中获取操作列表?动作列表通过 Index 动作中的 MyViewmodel 传入。

助手类 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());
        }
    }

控制器索引动作

 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; }
    }

看法

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

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

1 回答 1

1

actions 属性是一个列表PageAction。因此,您可以强制帮助器将 lambda 表达式作为参数,该表达式返回此自定义类型的列表,以便在帮助器内部您可以访问actionuser属性:

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());
    }
}
于 2012-07-02T06:26:36.657 回答