Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC CheckboxListFor values not posting back
    text
    copied!<p>In desperation, I created a CheckboxListFor method as follows (I ripped this off from somewhere, probably here).</p> <pre><code>public static MvcHtmlString CheckBoxListFor&lt;TModel, TProperty&gt;(this HtmlHelper&lt;TModel&gt; htmlHelper, Expression&lt;Func&lt;TModel, IEnumerable&lt;TProperty&gt;&gt;&gt; expression, MultiSelectList multiSelectList, object htmlAttributes = null) { //Derive property name for checkbox name var body = expression.Body as MemberExpression; string propertyName = body.Member.Name; //Get currently select values from the ViewData model IEnumerable&lt;TProperty&gt; list = expression.Compile().Invoke(htmlHelper.ViewData.Model); //Convert selected value list to a List&lt;string&gt; for easy manipulation var selectedValues = new List&lt;string&gt;(); if (list != null) { selectedValues = new List&lt;TProperty&gt;(list).ConvertAll(i =&gt; i.ToString()); } //Create div var divTag = new TagBuilder("div"); divTag.MergeAttributes(new RouteValueDictionary(htmlAttributes), true); //Add checkboxes foreach (SelectListItem item in multiSelectList) { divTag.InnerHtml += String.Format("&lt;div&gt;&lt;input type=\"checkbox\" name=\"{0}\" id=\"{0}_{1}\" " + "value=\"{1}\" {2} /&gt;&lt;label for=\"{0}_{1}\"&gt;{3}&lt;/label&gt;&lt;/div&gt;", propertyName, item.Value, selectedValues.Contains(item.Value) ? "checked=\"checked\"" : "", item.Text); } return MvcHtmlString.Create(divTag.ToString()); } </code></pre> <p>I call it like so:</p> <pre><code> &lt;h2&gt;Rights&lt;/h2&gt; &lt;div&gt; @Html.CheckBoxListFor(m=&gt;m.SelectedRights, Model.Rights) &lt;/div&gt; </code></pre> <p>And it renders out correctly. However, the selected values do not post back. How do I go about fixing this? Can anybody suggest a good starting point?</p> <p><strong>Update:</strong> I thought it might be good to add some more detail, as this thing may have multiple issues.</p> <p>The ids of the checkboxes are coming out like "SelectedRights_6". Here's the ViewModel I'm currently using.</p> <pre><code> public class RoleSingleViewModel : BaseEntitySingleViewModel { public override int ListViewColSpan { get { return 6; } } public string Name {get;set;} public string Description {get;set;} public IEnumerable&lt;SelectListItem&gt; Sites {get;set;} public int SiteId{get;set;} public string SiteText{get;set;} public override string TypeName { get { return "Role"; } } public MultiSelectList Rights { get{return new MultiSelectList(AllRights, "Key", "Value", SelectedRights);}} public List&lt;KeyValuePair&lt;int, string&gt;&gt; AllRights { get; set; } public List&lt;KeyValuePair&lt;int, string&gt;&gt; SelectedRights { get; set; } } </code></pre> <p>Also, the update method on the controller is like so. I set a breakpoint on the line where the exception is thrown and examined updateItem. The value of Rights, AllRights, and SelectedRights are not filled in (which is, I guess, expected behavior). I'm thinking my problem might be with the structure of my ViewModel. How should I change it?</p> <pre><code>[HttpPost] public ActionResult Edit(RoleSingleViewModel updateItem) { throw new NotImplementedException(); } </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