Advertisement
Guest User

Untitled

a guest
Oct 17th, 2012
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.54 KB | None | 0 0
  1.     public static class RaidioButtonListHelper
  2.     {
  3.         /// <summary>
  4.         /// Create a radiobutton list from viewmodel.
  5.         /// </summary>
  6.         public static MvcHtmlString RadioButtonList<TModel, TResult>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TResult>> expression, IEnumerable<SelectListItem> listOfValues = null)
  7.         {
  8.             var typeOfProperty = expression.ReturnType;
  9.             var underlyingType = Nullable.GetUnderlyingType(typeOfProperty);
  10.             if (underlyingType != null)
  11.                 typeOfProperty = underlyingType;
  12.             if (listOfValues == null && typeOfProperty.IsEnum)
  13.                 listOfValues = new SelectList(Enum.GetValues(typeOfProperty));
  14.  
  15.             var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
  16.  
  17.             // Ctreat table
  18.             TagBuilder tableTag = new TagBuilder("table");
  19.             tableTag.AddCssClass("radio-main");
  20.  
  21.             // Create tr:s
  22.             var trTagLable = new TagBuilder("tr id=\"" + metaData.PropertyName + "Lables\"");
  23.             var trTagRadio = new TagBuilder("tr id=\"" + metaData.PropertyName + "Radios\"");
  24.  
  25.             foreach (SelectListItem item in listOfValues)
  26.             {
  27.                 var text = item.Text;
  28.                 var value = item.Value ?? text;
  29.  
  30.                 // Generate an id to be given to the radio button field
  31.                 var id = string.Format("{0}_{1}", metaData.PropertyName, value);
  32.  
  33.                 // Create the radiobuttons
  34.                 var radioTag = htmlHelper.RadioButtonFor(expression, value, new { id = id }).ToHtmlString();
  35.  
  36.                 // Create the label for the radiobuttons.
  37.                 var labelTag = htmlHelper.Label(id, text);
  38.  
  39.                 // Add the lables and reaiobuttons to td:s
  40.                 var tdTagLable = new TagBuilder("td style=\"padding-left: 10px; text-align: center\"");
  41.                 var tdTagRadio = new TagBuilder("td style=\"padding-left: 10px; text-align: center\"");
  42.                 tdTagLable.InnerHtml = labelTag.ToString();
  43.                 tdTagRadio.InnerHtml = radioTag.ToString();
  44.  
  45.                 // Add tds: to tr:s
  46.                 trTagLable.InnerHtml += tdTagLable.ToString();
  47.                 trTagRadio.InnerHtml += tdTagRadio.ToString();
  48.  
  49.             }
  50.  
  51.             // Add tr:s to table
  52.             tableTag.InnerHtml = trTagLable.ToString() + trTagRadio.ToString();
  53.  
  54.             //Return the table tag
  55.             return new MvcHtmlString(tableTag.ToString());
  56.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement