MVC Checkboxlist


/ Published in: C#
Save to your folder(s)

Code snippet that allows developers to use a generic radiobuttonlist in the MVC Framework


Copy this code and paste it in your HTML
  1. public static void ShowCheckBoxList<T>(this ViewPage page, IList<string> list, string name, Expression<Func<T, object>> valueProperty, Expression<Func<T, object>> displayProperty, List<string> selectedValue, System.Web.UI.WebControls.Orientation orientation)
  2. {
  3. HtmlTextWriter writer = new HtmlTextWriter(page.Response.Output);
  4. if (writer != null)
  5. {
  6. for (int i = 0; i < list.Count; i++)
  7. {
  8. string value = list[i];
  9. writer.AddAttribute(HtmlTextWriterAttribute.Type, "CHECKBOX");
  10. writer.AddAttribute(HtmlTextWriterAttribute.Id, name + "_" + i);
  11. writer.AddAttribute(HtmlTextWriterAttribute.Name, name, true);
  12. writer.AddAttribute(HtmlTextWriterAttribute.Value, value, true);
  13. if (selectedValue != null &&selectedValue.Contains(value))
  14. writer.AddAttribute(HtmlTextWriterAttribute.Checked, "checked");
  15.  
  16. writer.RenderBeginTag(HtmlTextWriterTag.Input);
  17. writer.Write(value);
  18. writer.RenderEndTag();
  19.  
  20. if (orientation == System.Web.UI.WebControls.Orientation.Vertical)
  21. {
  22. writer.Write("<br/>");
  23. }
  24. }
  25. }
  26. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.