我有一個下拉列表,通過EditorTemplate呈現。該屬性在Validation類中具有UIHints並正確顯示,但是當我查看HTML時,控件的名稱是PropertyType.PropertyName而不僅僅是PropertyName。MVC DropDownList與EditorTemplate顯示選定的值和無效的名稱字段作爲propertyName.propertyName
這可以防止模型綁定。
我也無法將選定的值返回到視圖。
我該如何解決這個問題?
UPDATE有關詳細信息,請參閱下面的答案。
視圖模型
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; }
}
下拉視圖模型
public class HousingTypeDropDownViewModel
{
public int housingTypeID { get; set; }
public IEnumerable<SelectListItem> Items { get; set; }
}
EditorTemplate
@model WhatWorks.ViewModels.HousingTypeDropDownViewModel
@Html.DropDownListFor(h => h.housingTypeID, new SelectList(Model.Items, "Value", "Text"))
查看
using (Html.ControlGroupFor(property.Name))
{
@Html.Label(property.GetLabel(), new { @class = "control-label" })
<div class="controls">
@Html.Editor(property.Name, new { @class = "input-xlarge" })
@Html.ValidationMessage(property.Name, null, new { @class = "help-inline" })
</div>
}
HTML
<div class="control-group">
<label class="control-label" for="Housing_Type">Housing Type</label>
<div class="controls">
<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">
<option value="1">Owner Occupied</option>
<option value="2">Rented - Social Landlord</option>
</select>
<span class="field-validation-valid help-inline" data-valmsg-for="housingType" data-valmsg-replace="true"></span>
</div>
</div>