2011-12-29 88 views
0

我有一個報告,顯示有以下的列的列表:MVC3 Razor中的DropDownList或DropDownListFor?

A key column 
A dropdown list showing a list of possible status 
A title 

這是剃刀代碼:

@foreach (AdminSummary adminSummary in @Model.AdminSummaries) { 
       index++; 
       <div class="rep_tr0"> 
        <div class="rep_td0" id="[email protected](index)">@adminSummary.RowKey</div> 
        <div class="rep_td0"><a href="/Administration/Products/Edit?&[email protected]&[email protected]&[email protected]">Edit</a></div>    
        <div class="rep_td0"><a href="/Administration/Products/Delete?&[email protected]&[email protected]&[email protected]">Delete</a></div> 
        <div class="rep_td0">@Html.DropDownListFor(??, ??)</div> 
        </div> 
      } 

我也有下面的類:

public static class AdminStatusReference 
    { 

     public static IEnumerable<SelectListItem> GetAdminStatusOptions() 
     { 
      return new[] 
       { 
        new SelectListItem { Value = "1", Text = "Released" }, 
        new SelectListItem { Value = "2", Text = "Review" }, 
        new SelectListItem { Value = "3", Text = "New" } 
       }; 
     } 

} 

什麼我不確定我是如何將這些鏈接起來的。我知道在我的模型可以在 狀態選項的東西列表存儲這樣的:

vm.StatusList = GetAdminStatusOptions(); 

但我怎麼可以創建下拉列表。我對DropDownList和DropDownListFor的所有選項都很困惑。我應該使用哪種方式以及如何發送狀態列表?

回答

1

我不確定是如何將這些鏈接起來。

這取決於模型的外觀。

但在這裏你可以做什麼:

@for (var index = 0; index < Model.AdminSummaries.Count; index++) 
{ 
    <div class="rep_tr0"> 
     <div class="rep_td0" id="[email protected](index)"> 
      @Model.AdminSummaries[index].RowKey 
     </div> 
     <div class="rep_td0"> 
      @Html.DropDownListFor(
       x => x.AdminSummaries[index].Status, 
       AdminStatusReference.GetAdminStatusOptions() 
      ) 
     </div> 
     <div class="rep_td0"> 
      @Model.AdminSummaries[index].Title 
     </div> 
    </div> 
} 
+0

感謝達林,我只是忙着糾正我的問題,你已經給了我一個答案。我現在檢查一下。 – 2011-12-29 12:49:07

+0

對不起,花了這麼長的時間回到你身邊。 AdminSummaries如下所示:public IEnumerable AdminSummaries {get;組; }當我嘗試運行代碼時,出現運行時錯誤消息:編譯器錯誤消息:CS0021:無法將[]的索引應用於類型爲'System.Collections.Generic.IEnumerable '的表達式。 – 2011-12-29 13:30:56

+0

@ Samantha2,好的,將其更改爲IList 或AdminSummary [],以便您可以通過索引訪問元素。 – 2011-12-29 13:32:29