Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC3 Data binding
    primarykey
    data
    text
    <p>I have a partial view which I pass a model to and it displays checkboxs which the user can select and this eventually gets posted with my form so that I can query their checked state and insert data into DB as required.</p> <p>I want to change my partial view so that instead of taking a model that contains a list of models it take the list of models directly thus decoupling it and making it more useful as a partial view. The problem is when I do this it doesn't seem to bind the data correctly. Below is an example of the code before (working correctly and after not working).</p> <p>I would very much appreciate if somebody is able to shed some light as to why my changes are not taking affect, i.e. in the after code when I inspect the posted data the list is null and in the before it is populated correctly:</p> <p><strong>Before:</strong></p> <p>Call the partial view:</p> <pre><code>&lt;fieldset&gt; &lt;legend&gt;Facilities&lt;/legend&gt; &lt;div class="display-field"&gt; &lt;div&gt; @{Html.RenderPartial("Partial/FacilityPartial", Model);} &lt;/div&gt; &lt;/div&gt; &lt;/fieldset&gt; </code></pre> <p>Partial view:</p> <pre><code>@model namespace.Models.SchoolRegisterModel @for (var i = 0; i &lt; Model.Facilities.Count; i++) { @Html.HiddenFor(x =&gt; x.Facilities[i].name) @Html.HiddenFor(x =&gt; x.Facilities[i].facility_id) @Html.LabelFor(x =&gt; x.Facilities[i].@checked, Model.Facilities[i].name); @Html.CheckBoxFor( x =&gt; x.Facilities[i].@checked, new { id = Model.Facilities[i].facility_id, @class = "RightSpacing", description = Model.Facilities[i].description } ) } </code></pre> <p><strong>After:</strong></p> <p>Call the partial view:</p> <pre><code>&lt;fieldset&gt; &lt;legend&gt;Facilities&lt;/legend&gt; &lt;div class="display-field"&gt; &lt;div&gt; @{Html.RenderPartial("Partial/FacilityPartial", Model.Facilities);} &lt;/div&gt; &lt;/div&gt; &lt;/fieldset&gt; </code></pre> <p>Partial view:</p> <pre><code>@model IEnumerable&lt;namespace.Models.facility&gt; @for (int i = 0; i &lt; Model.Count(); i++) { @Html.HiddenFor(x =&gt; x.ElementAt(i).name) @Html.HiddenFor(x =&gt; x.ElementAt(i).name) @Html.HiddenFor(x =&gt; x.ElementAt(i).facility_id) @Html.LabelFor(x =&gt; x.ElementAt(i).@checked, Model.ElementAt(i).name); @Html.CheckBoxFor( x =&gt; x.ElementAt(i).@checked, new { id = Model.ElementAt(i).facility_id, @class = "RightSpacing", description = Model.ElementAt(i).description } ) } </code></pre> <p>Thanks in advance! </p> <p>Joe</p> <p><strong>Finished Code (Working)</strong></p> <p>Model:</p> <pre><code>public class SchoolRegisterModel { // ... Lots of data public string Data { get; set; } [Display(Name = "Facilities")] public IEnumerable&lt;FacilityViewModel&gt; Facilities { get; set; } } namespace namespace.Models { public class FacilityViewModel { public FacilityViewModel() { } public FacilityViewModel(facility facil, bool bSelected = false) { this.Facility = facil; this.Selected = bSelected; } public facility Facility { get; set; } public bool Selected { get; set; } } } </code></pre> <p>Editor Template:</p> <pre><code>@model namespace.Models.FacilityViewModel @Html.HiddenFor(x =&gt; x.Facility.facility_id) @Html.HiddenFor(x =&gt; x.Facility.name) @Html.LabelFor(x =&gt; x.Selected, Model.Facility.name) @Html.CheckBoxFor(x =&gt; x.Selected, new { id = Model.Facility.facility_id, @class = "RightSpacing", description = Model.Facility.description } ) </code></pre> <p>Controller:</p> <pre><code>// GET: /School/Register public ActionResult Register() { var viewModel = new SchoolRegisterModel(); PopulateFacilityCollection(ref viewModel); return View(viewModel); } private void PopulateFacilityCollection(ref SchoolRegisterModel viewModel) { List&lt;FacilityViewModel&gt; FacilityCollection = new List&lt;FacilityViewModel&gt;(); foreach (facility facil in DBModel.facilities) { FacilityCollection.Add(new FacilityViewModel(facil)); } viewModel.Facilities = FacilityCollection; } </code></pre> <p>View:</p> <pre><code>&lt;fieldset&gt; &lt;legend&gt;Facility&lt;/legend&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(x =&gt; x.Facilities) &lt;/div&gt; &lt;/fieldset&gt; </code></pre> <p>All seems to work well now, I hope that this helps somebody else! </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.
 

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