Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC DropDownList with EditorTemplate showing Selected Value and invalid name field as propertyName.propertyName
    primarykey
    data
    text
    <p>I have a dropdown list that is rendered through an EditorTemplate. The property has UIHints in a Validation class and displays correctly but when I look at the HTML the name of the control is PropertyType.PropertyName rather than just PropertyName.</p> <p>This prevents the model from binding.</p> <p>I also can't return a selected value to the View.</p> <p>How do I get around this?</p> <p><strong>UPDATE</strong> See answer below for details.</p> <p><strong>ViewModel</strong></p> <pre><code>public partial class HouseholdEditViewModel { public int householdID { get; set; } public int familyID { get; set; } public string address { get; set; } public HousingTypeDropDownViewModel housingType { get; set; } public KeyworkerDropDownViewModel keyworker { get; set; } public string attachmentDate { get; set; } public bool loneParent { get; set; } public string familyPhoneCode { get; set; } public string familyPhone { get; set; } } </code></pre> <p><strong>DropDown ViewModel</strong></p> <pre><code>public class HousingTypeDropDownViewModel { public int housingTypeID { get; set; } public IEnumerable&lt;SelectListItem&gt; Items { get; set; } } </code></pre> <p><strong>EditorTemplate</strong></p> <pre><code>@model WhatWorks.ViewModels.HousingTypeDropDownViewModel @Html.DropDownListFor(h =&gt; h.housingTypeID, new SelectList(Model.Items, "Value", "Text")) </code></pre> <p><strong>View</strong></p> <pre><code>using (Html.ControlGroupFor(property.Name)) { @Html.Label(property.GetLabel(), new { @class = "control-label" }) &lt;div class="controls"&gt; @Html.Editor(property.Name, new { @class = "input-xlarge" }) @Html.ValidationMessage(property.Name, null, new { @class = "help-inline" }) &lt;/div&gt; } </code></pre> <p><strong>HTML</strong></p> <pre><code>&lt;div class="control-group"&gt; &lt;label class="control-label" for="Housing_Type"&gt;Housing Type&lt;/label&gt; &lt;div class="controls"&gt; &lt;select data-val="true" data-val-number="The field housingTypeID must be a number." data-val-required="The housingTypeID field is required." id="housingType_housingTypeID" name="housingType.housingTypeID"&gt; &lt;option value="1"&gt;Owner Occupied&lt;/option&gt; &lt;option value="2"&gt;Rented - Social Landlord&lt;/option&gt; &lt;/select&gt; &lt;span class="field-validation-valid help-inline" data-valmsg-for="housingType" data-valmsg-replace="true"&gt;&lt;/span&gt; &lt;/div&gt; &lt;/div&gt; </code></pre>
    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.
 

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