2012-07-02 67 views
1

我想創建一個強類型的單選按鈕列表。我不知道如何從HtmlExtensionClass中的ModelMetaData中獲取動作列表?操作列表通過Index操作中的MyViewmodel傳入。如何在asp.net mvc 3中創建一個強類型的radiobuttonlist?

助手類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) 
} 

回答

1

的行動屬性是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()); 
    } 
} 
+0

十分感謝達林! – user603007

相關問題