Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.Net MVC 3 - CheckBoxList - Need some suggestions
    text
    copied!<p>I am pretty new to ASP.Net MVC (and razor) and I have a few questions.</p> <p>1) </p> <p>I created an HTML extension to create a check box list as below:</p> <pre><code>public static HtmlString CheckBoxList(this HtmlHelper htmlHelper, string name, List&lt;InputItemInfo&gt; ItemInfo) { if (String.IsNullOrEmpty(name)) throw new ArgumentException("The argument must have a value", "name"); if (ItemInfo == null) throw new ArgumentNullException("ItemInfo"); if (ItemInfo.Count &lt; 1) throw new ArgumentException("The list must contain at least one value", "ItemInfo"); StringBuilder sb = new StringBuilder(); ItemInfo.Insert(0, new InputItemInfo("*", "Select All", ItemInfo.All(i =&gt; i.IsChecked))); foreach (InputItemInfo info in ItemInfo) { TagBuilder builder = new TagBuilder("input"); if (info.IsChecked) builder.MergeAttribute("checked", "checked"); builder.MergeAttribute("type", "checkbox"); builder.MergeAttribute("value", info.Value); builder.MergeAttribute("name", name); builder.InnerHtml = info.DisplayText; sb.Append(builder.ToString(TagRenderMode.Normal)); sb.Append("&lt;br /&gt;"); } return new HtmlString(sb.ToString()); } </code></pre> <p>I was able to use this in my views and also get the values in the controller as shown below:</p> <pre><code>@model List&lt;AppTest.Models.InputExtensionsViewModel&gt; @{ ViewBag.Title = "Check"; } &lt;h2&gt;Check&lt;/h2&gt; @using (Html.BeginForm()) { &lt;table border="0" style="border:0px;"&gt; &lt;tr&gt; &lt;td valign="top"&gt; @Html.Partial("CheckBoxList", Model[0]) &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;br /&gt; &lt;input type="submit" value="Go" /&gt; } &lt;div style="font-weight:bolder"&gt; @ViewData["data"] &lt;/div&gt; </code></pre> <p>controller:</p> <pre><code>public ActionResult Check() { var model = new List&lt;InputExtensionsViewModel&gt;(); var model1 = new InputExtensionsViewModel { Title = "Facilities", InputElementName = "facilities", InputElements = // a list }; model.Add(model1); return View(model); } [HttpPost] public ActionResult Check(string[] facilities) { ... } </code></pre> <p>The model is:</p> <pre><code>public class InputExtensionsViewModel { public string Title { get; set; } public string InputElementName { get; set; } public List&lt;InputItemInfo&gt; InputElements { get; set; } public void SetSelected(string[] items) { if (items == null) return; this.InputElements.ForEach(delegate(InputItemInfo info) { if (items.Contains(info.Value)) info.IsChecked = true; }); } } </code></pre> <p>My question is, is there a way by which I could bind the array items to a property in the InputExtensionsViewModel model? If I just add a property called facilities to the view model, it's not bound automatically and I can understand why, as I am not binding that in my view. But I cannot seem to think of a way by which I could do that.</p> <p>This check box list is a user control and I just wanted to avoid having too many string[] array for my action methods.</p> <p><em>[EDIT] - Okay, I was able to do this when I tried now. Not sure why it didn't work before.</em></p> <p>2) And, I was checking for alternatives and found out this answer in SO:</p> <p><a href="https://stackoverflow.com/questions/4872192/checkboxlist-in-mvc3-0">CheckboxList in MVC3.0</a></p> <p>And I was able to replicate this but my question is, how do i bind a label to this checkbox? My labels are dynamic and part of the model and so cannot be hard-coded. I was trying to use a Html.LabelFor but that didn't work. In the editor template, if I just @Model.Text, it won't work and will be lost after a post-back as its not bound to a property</p> <p>I googled and found suggestions to create HTML helpers which is what I did earlier (my 1st question is about that).</p> <p>Please let me know if something is unclear. I could elaborate. Any input is appreciated!</p> <p>Thanks in advance!</p>
 

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