Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Behold the final solution:</p> <pre><code>public static class Helpers { public static string CheckboxGroup&lt;TModel, TProperty&gt;(this HtmlHelper&lt;TModel&gt; htmlHelper, Expression&lt;Func&lt;TModel, TProperty&gt;&gt; propertySelector, int value) where TProperty: IEnumerable&lt;int&gt; { var groupName = GetPropertyName(propertySelector); var modelValues = propertySelector.Compile().Invoke(htmlHelper.ViewData.Model); var svalue = value.ToString(); var builder = new TagBuilder("input"); builder.GenerateId(groupName); builder.Attributes.Add("type", "checkbox"); builder.Attributes.Add("name", groupName); builder.Attributes.Add("value", svalue); var contextValues = HttpContext.Current.Request.Form.GetValues(groupName); if ((contextValues != null &amp;&amp; contextValues.Contains(svalue)) || (modelValues != null &amp;&amp; modelValues.Contains(value))) { builder.Attributes.Add("checked", null); } return builder.ToString(TagRenderMode.Normal); } private static string GetPropertyName&lt;T, TProperty&gt;(Expression&lt;Func&lt;T, TProperty&gt;&gt; propertySelector) { var body = propertySelector.Body.ToString(); var firstIndex = body.IndexOf('.') + 1; return body.Substring(firstIndex); } } </code></pre> <p>And in your view:</p> <pre><code> &lt;%= Html.CheckboxGroup(model =&gt; model.DocumentCoverCustom, "1")%&gt;(iv),&lt;br /&gt; &lt;%= Html.CheckboxGroup(model =&gt; model.DocumentCoverCustom, "2")%&gt;(vi),&lt;br /&gt; &lt;%= Html.CheckboxGroup(model =&gt; model.DocumentCoverCustom, "3")%&gt;(vii),&lt;br /&gt; &lt;%= Html.CheckboxGroup(model =&gt; model.DocumentCoverCustom, "4")%&gt;(ix)&lt;br /&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload