Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I ended up using the EditorTemplate functionality in order to solve / render this.</p> <p>The Model changed from this:</p> <pre><code>public class ValueModel { public Guid Id { get; set; } public string Name { get; set; } } </code></pre> <p>To this:</p> <pre><code>public class ValueModel { public Guid Id { get; set; } public string Name { get; set; } public List&lt;ValueDropDownModel&gt; DropDown { get; set; } } public class ValueDropDownModel { [Range(0, 15)] public int DropDown { get; set; } public int Id { get; set; } public string Name { get; set; } public List&lt;SelectListItem&gt; AvailableDropDown { get; set; } } </code></pre> <p>Within the <code>Views</code> under the folder for <code>Value</code>, there is now a sub folder called <code>EditorTemplates</code>. This contains 2 Views. A <code>ValueModel.cshtml</code> and a <code>ValueDropDownModel.cshtml</code>.</p> <p><code>ValueModel.cshtml</code> contains:</p> <pre><code>@model SomeSolution.Web.Models.ValueModel &lt;div class="row-fluid"&gt; &lt;div class="span6"&gt; &lt;div class="control-group"&gt; @Html.LabelFor(m =&gt; m.Name) &lt;div class="controls"&gt; @Html.EditorFor(m =&gt; m.Name) @Html.ValidationMessageFor(m =&gt; m.Name) &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="row-fluid"&gt; &lt;div class="span4"&gt; @for (int i = 0; i &lt; 5; ++i) { @Html.EditorFor(m =&gt; m.DropDown[i]) } &lt;/div&gt; &lt;div class="span4"&gt; @for (int i = 5; i &lt; 10; ++i) { @Html.EditorFor(m =&gt; m.DropDown[i]) } &lt;/div&gt; &lt;div class="span4"&gt; @for (int i = 10; i &lt; 15; ++i) { @Html.EditorFor(m =&gt; m.DropDown[i]) } &lt;/div&gt; &lt;/div&gt; </code></pre> <p><code>ValueDropDownModel.cshtml</code> contains:</p> <pre><code>@model SomeSolution.Web.Models.ValueDropDownModel &lt;div class="control-group"&gt; @Html.HiddenFor(m =&gt; m.DropDown) @String.Format("DropDown {0}", Model.DropDown) &lt;div class="controls"&gt; @Html.DropDownListFor(m =&gt; m.Id, Model.AvailableDropDown) @Html.ValidationMessageFor(m =&gt; m.Id) &lt;/div&gt; &lt;/div&gt; </code></pre> <p>The <code>ValueController</code> now includes some helper methods to populate the drop downs</p> <pre><code> private void FillAvailableValues(ValueModel modelValue, Entities db) { var values = (from v in db.Values orderby v.Name select v); foreach (var model in modelValue.Values) { model.AvailableDropDown = new List&lt;SelectListItem&gt;(); model.AvailableDropDown.Add(new SelectListItem() { Text = "Unassigned", Value = "0", Selected = (model.Id == 0) }); foreach (var value in values) { model.AvailableDropDown.Add(new SelectListItem() { Text = value.Name, Value = value.Id, Selected = (model.Id.ToString() == colour.Id) }); } } } private void InitDefaultDropDown(ValueModel model) { model.DropDown = new List&lt;ValueDropDownModel&gt;(); for (int i = 0; i &lt; 15; i++) { model.DropDown.Add(new ValueDropDownModel() { DropDown = i + 1, Id = 0 }); } } </code></pre> <p>The <code>FillAvailableValues</code> method is called on the <code>Create</code> <code>ActionResult</code> as well as the <code>Edit</code> <code>ActionResult</code> to initialize the Drop Downs. The <code>InitDefaultDropDown</code> method is called on the <code>Create</code> <code>ActionResult</code> to setup the Drop Downs on the page.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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