2014-11-04 80 views
0

我有三個dropdownlist在循環中不顯示數據庫的正確值。他們總是默認爲第一個條目。我已經檢查並雙重檢查了數據庫並確認它應該是列表中的第二個。該列表也是正確創建的。我錯過了什麼?Dropdownlistfor不會顯示正確的選擇

 @foreach (CustomerMeasurementProfile oProfile in Model.Customer.CustomerMeasurementProfiles.Where(m => m.DeletedDate == null)) 
     { 

      <div class="valuesforoneprofile form-group form-group-tight col-md-2"> 
       <div class="col-md-11" > 
        @Html.Hidden(string.Format("Customer.CustomerMeasurementProfiles[{0}].Id", i), oProfile.Id) 
        @Html.Hidden(string.Format("Customer.CustomerMeasurementProfiles[{0}].CustomerId", i), oProfile.CustomerId) 
        @Html.TextBox(string.Format("Customer.CustomerMeasurementProfiles[{0}].Name", i), oProfile.Name, new { @class = "form-control input-sm" }) 
       </div> 
       <div class="col-md-11" style="text-align:center"> 
        @Html.CheckBox(string.Format("DeleteProfiles[{0}]", i), Model.DeleteProfiles[i]) 
       </div> 
       <div class="col-md-11" style="padding-top:4px;"> 
        @Html.DropDownListFor(m => oProfile.BodyTypeShoulderId, new SelectList(Model.BodyTypeShoulders, "Id", "Name"), new { @class = "form-control input-sm-select" }) 
       </div> 
       <div class="col-md-11" style="padding-top:4px;"> 
        @Html.DropDownListFor(m => oProfile.BodyTypePostureId, new SelectList(Model.BodyTypePosture, "Id", "Name"), new { @class = "form-control input-sm-select" }) 
       </div> 
       <div class="col-md-11" style="padding-top:4px;"> 
        @Html.DropDownListFor(m => oProfile.BodyTypeChestId, new SelectList(Model.BodyTypeChest, "Id", "Name"), new { @class = "form-control input-sm-select" }) 
       </div> 

回答

2

如果你想設置選定值在模式到來。你需要做的是這樣的:

@Html.DropDownListFor(m => oProfile.BodyTypeShoulderId, 
          new SelectList(Model.BodyTypeShoulders, 
              "Id", 
              "Name", 
              oProfile.BodyTypeShoulderId), 
          new { @class = "form-control input-sm-select" }) 

上面的代碼將設置的下拉列表中選擇值無論是在當前模式對象BodyTypeShoulderId

DropDownListFor的第一個參數告訴在表格後降向下選擇的值將與在那裏設置的Model屬性(我們正在通過m => oProfile.BodyTypeShoulderId)映射,但這不會設置選定的值。

用於設置選擇的值,你必須通過使用this overload of SelectList classSelectList第四個參數是object selectedValue

+0

謝謝,完美的作品! – Craig 2014-11-04 18:29:06

+0

歡迎並樂於提供幫助 – 2014-11-04 18:35:26

0

不幸的是@Html.DropDownListFor()在循環中呈現控件時比其他傭工的行爲有點不同。對於單個對象

@Html.DropDownListFor(m => oProfile.BodyTypeShoulderId, new SelectList(Model.BodyTypeShoulders, "Id", "Name")) 

會很好地工作(如果BodyTypeShoulderId值的選項之一的值匹配,則該選項將被選擇)。 Ehsan在循環中使用它時已經顯示了一個解決方法,但是在代碼中還有一些其他問題,並非最不重要的是,許多屬性不會正確回發到集合,因爲您使用的是循環而不是for循環(這是生成重複nameid屬性)。您也在循環的每次迭代中生成一個新的SelectList,這不是非常有效。

您可以通過使用EditorTemplate來解決這些問題和下拉選擇問題。假設屬性CustomerMeasurementProfiles是typeof運算CustomerMeasurementProfiles,然後

CustomerMeasurementProfiles.cshtml(這增加Views/Shared/EditorTemplatesViews/YourController/EditorTemplates

@model CustomerMeasurementProfiles 
@Html.HiddenFor(m => m.ID) 
@Html.HiddenFor(m => m.CustomerId) 
.... 
@Html.DropDownListFor(m => m.BodyTypeShoulderId, (SelectList)ViewData["shoulders"]) 
.... 

在視圖模式,爲SelectList的添加屬性和項目的過濾收集,以顯示

IEnumerable<CustomerMeasurementProfiles> ActiveCustomerMeasurementProfiles { get; set; } 
public SelectList BodyTypeShoulderList { get; set; } 
.... 

,並在控制器指定這些值

然後在主視圖

@using (Html.BeginForm()) 
{ 
    ... 
    @Html.EditorFor(m => m.ActiveCustomerMeasurementProfiles, new { shoulders = Model.BodyTypeShoulderList }) 
    ... 

使用@Html.EditorFor()與收集將正確地命名控制(和正確選擇正確的選項)和回傳,ActiveCustomerMeasurementProfiles現在將正確填入其所有屬性。