Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC 3 CheckboxList and me...Part 3. Try specifying the type arguments explicitly
    primarykey
    data
    text
    <p>This IS fun.</p> <p>Ok, I have the following <strong>model(s)</strong>:</p> <pre><code>public class My : BusinessCategory { [Display(Name = "What types of energy do you use?")] public List&lt;MyTypes&gt; MyTypeList { get; set; } public bool? FirstOption { get; set; } public bool? SecondOption{ get; set; } public bool? ThirdOption{ get; set; } public bool? FourthOption { get; set; } </code></pre> <p>}</p> <p>Where MyTypes:</p> <pre><code>public class MyTypes { public int MyTypeId { get; set; } public string MyTypeName { get; set; } public bool? MyTypeValue { get; set; } } </code></pre> <p>My controller is as follows:</p> <pre><code>public ActionResult My(Guid id) { try { var model = Model(id); SetMyTypeList(model.My); ViewBag.MyTypeMe = new MultiSelectList(model.My.MyTypeList, "MyTypeValue", "MyTypeName"); return View(model.My); } catch (Exception ex) { ExceptionHelper.WriteLog(ex); return RedirectToAction("Error"); } } private void SetMyTypeList(My model) { model.MyTypeList = new List&lt;MyTypes&gt;(); model.MyTypeList.Add(new MyTypes { MyTypeId = 1, MyTypeName = GetName.GetDisplayName(model, m =&gt; m.FirstOption), MyTypeValue = model.FirstOption }); model.MyTypeList.Add(new MyTypes { MyTypeId = 2, MyTypeName = GetName.GetDisplayName(model, m =&gt; m.SecondOption), MyTypeValue = model.SecondOption}); model.MyTypeList.Add(new MyTypes { MyTypeId = 3, MyTypeName = GetName.GetDisplayName(model, m =&gt; m.ThirdOption), MyTypeValue = model.ThirdOption}); model.MyTypeList.Add(new MyTypes { MyTypeId = 4, MyTypeName = GetName.GetDisplayName(model, m =&gt; m.FourthOption), MyTypeValue = model.FourthOption }); } public static string GetDisplayName&lt;TModel, TProperty&gt;(TModel model, Expression&lt;Func&lt;TModel, TProperty&gt;&gt; expression) { return ModelMetadata.FromLambdaExpression&lt;TModel, TProperty&gt;(expression, new ViewDataDictionary&lt;TModel&gt;(model)).DisplayName; } </code></pre> <p>And finally the view is as follows:</p> <pre><code>@model Valpak.Websites.HealthChecker.Models.My @{ ViewBag.Title = "My"; } &lt;h2&gt; My&lt;/h2&gt; @using (Html.BeginForm()) { @Html.ValidationSummary(true) &lt;fieldset&gt; &lt;legend&gt;My Management&lt;/legend&gt; &lt;div style="text-align: left; padding-left: 47%;"&gt; @Html.ListBoxFor(model =&gt; model.MyTypeList, ViewBag.MyTypeMe as MultiSelectList) @Html.CheckBoxListFor(model =&gt; model.MyTypeList, ViewBag.EnergyTypeMe as MultiSelectList, Model.ReviewId) &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Continue" /&gt; &lt;/p&gt; &lt;/fieldset&gt; } &lt;div&gt; @Html.ActionLink("Cancel and return to categories", "BusinessSummary", new { id = Model.ReviewId }) &lt;/div&gt; </code></pre> <p>CheckboxListFor, if it was working, would use the following extension:</p> <pre><code>public static class HtmlHelper { //Extension public static MvcHtmlString CheckBoxListFor&lt;TModel, TProperty&gt;(this HtmlHelper&lt;TModel&gt; htmlHelper, Expression&lt;Func&lt;TModel, TProperty[]&gt;&gt; expression, MultiSelectList multiSelectList, object htmlAttributes = null) { //Derive property name for checkbox name MemberExpression body = expression.Body as MemberExpression; string propertyName = body.Member.Name; //Get currently select values from the ViewData model TProperty[] list = expression.Compile().Invoke(htmlHelper.ViewData.Model); //Convert selected value list to a List&lt;string&gt; for easy manipulation List&lt;string&gt; selectedValues = new List&lt;string&gt;(); if (list != null) { selectedValues = new List&lt;TProperty&gt;(list).ConvertAll&lt;string&gt;(delegate(TProperty i) { return i.ToString(); }); } //Create div TagBuilder 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>Can someone you explain in very simplistic terms (I’m a bit dense) why I can use the ListBoxFor example but this dies and gives me the following error when I use the checkbox?</p> <pre><code>CS0411: The type arguments for method 'Extensions.HtmlHelper.CheckBoxListFor&lt;TModel,TProperty&gt;(System.Web.Mvc.HtmlHelper&lt;TModel&gt;, System.Linq.Expressions.Expression&lt;System.Func&lt;TModel,TProperty[]&gt;&gt;, System.Web.Mvc.MultiSelectList, System.Guid, object)' cannot be inferred from the usage. Try specifying the type arguments explicitly. </code></pre> <p>Can anyone offer any sort of work around as I’d quite like to use my :’(</p> <p>As always, apologies for my ignorance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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