2013-10-04 52 views
1

我有一個下拉列表,通過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> 

回答

0

已經研究這個多一點,似乎,這是如預期運行。

最簡單的解決方法是在EditorTemplate中使用DropDownList而不是DropDownListFor。將name設置爲空字符串,並且Html.Editor僅拾取屬性名稱一次。另外要注意的SelectList

@model WhatWorks.ViewModels.HousingTypeDropDownViewModel 

@Html.DropDownList("", Model.Items) 

的變化Model.Items爲了進一步詳細描述了一下這個,我試圖用從我的ViewModel的IEnumerable<SelectListItem>一個DropDownList,並努力讓我的HTML標籤渲染正確。其中一部分涉及在我的編輯視圖上需要Selected Value

由於這似乎是一個常規的問題,我稍作修改,這篇文章的標題,並會在這裏記錄控制器代碼 - 這是什麼工作對我來說,可能不是最好的做法... 買者自負

IRepository

public interface IHouseholdRepository : IDisposable 
{ 
    IEnumerable<tHousehold> Get(); 
    tHousehold GetById(int Id); 
    IEnumerable<SelectListItem> AddHousingType(); 
    IEnumerable<SelectListItem> EditHousingType(int id); 
    //etc... 

} 

public IEnumerable<SelectListItem> AddHousingType() 
{ 
    var query = from ht in context.tHousingType 
       orderby ht.housingType 
       select new 
       { 
        ht.housingTypeID, 
        ht.housingType 
       }; 

    return query.AsEnumerable() 
     .Select(s => new SelectListItem 
     { 
      Value = s.housingTypeID.ToString(), 
      Text = s.housingType 
     }); 
} 

public IEnumerable<SelectListItem> EditHousingType(int id) 
{ 
    var housingType = (from h in context.tHousehold 
         where h.householdID == id 
         select new 
          { 
           h.tHousingStatus.FirstOrDefault().housingTypeID 
          } 
        ); 

    var query = from ht in context.tHousingType 
       orderby ht.housingType 
       select new 
       { 
        ht.housingTypeID, 
        ht.housingType 
       }; 

    return query.AsEnumerable() 
     .Select(s => new SelectListItem 
      { 
       Value = s.housingTypeID.ToString(), 
       Text = s.housingType, 
       Selected = (housingType.FirstOrDefault().housingTypeID == s.housingTypeID ? true : false) 
      }); 
} 

控制器

public ActionResult Edit(int id = 0) 
    { 
     HousingTypeDropDownViewModel housingType = new HousingTypeDropDownViewModel 
     { 
      Items = _repo.EditHousingType(id) 
     }; 

     HouseholdEditViewModel h = GetUpdate(id); 
     //GetUpdate is Automapper code unrelated to the DropDown 

     h.housingTypeID = housingType; 

     if (h == null) 
     { 
      return HttpNotFound(); 
     } 
     return View(h); 
    } 

啓示對上述從太多的SO帖子中提到。謝謝大家,並希望這有助於其他人。