VisualStudioFeeds

All Visual Studio blogs in one place

AddThis Social Bookmark Button
This has been done a few times, here's my attempt:         public static MvcHtmlString CheckBoxListFor(this HtmlHelper htmlHelper, Expression> expression) where TValue : IEnumerable         {             var name = expression.Body is ParameterExpression ? ((ParameterExpression)expression.Body).Name : ((MemberExpression)expression.Body).Member.Name;             var sb = new StringBuilder();             var values = ((IEnumerable)expression.Compile().Invoke(htmlHelper.ViewData.Model)).Select(x => x.ToString());             foreach (var value in values)             {                 sb.Append(string.Format("{0} ", value, name, value));             }             return new MvcHtmlString(sb.ToString());         } I included a ParameterExpression just in case the model is an IEnumerable so if your view looks like this: @model IEnumerable @using (Html.BeginForm()) {     @Html.CheckBoxListFor(items => items)      } Your action can do this:         [HttpPost]         public ActionResult Index(IEnumerable items)         {             return View("Result", items);         }  

Search

Subscribe

Enter your email address:

Delivered by FeedBurner

Increase your website traffic with Attracta.com

Contact

Email:
Subject:
Message:
Anti-spam: How many eyes has a typical person?

Follow us on FB

Home : Blog List : .Nettuce : MVC CheckBoxListFor Helper Extension