2012-01-01 47 views
0

我有以下代码的工作原理:如何在不使用lambda表达式的情况下使用Html.DropDownListFor?

@for (var index = 0; index < Model.AdminSummaries.Count(); index++) { 

    <div class="rep_td0" id="[email protected](index)"> 
     @Model.AdminSummaries[index].RowKey 
    </div> 
    <div class="rep_td0"> 
    @Html.DropDownListFor(
     x => x.AdminSummaries[index].Status, 
     new SelectList(
      AdminStatusReference.GetAdminStatusOptions(), 
      "Value", 
      "Text", 
    ), 
     new { id = string.Format("Status_{0}", index) } 
    ) 

我遍历Model.AdminSummaries这是一种类型的

public IList<AdminSummary> AdminSummaries { get; set; } 

由于它是一个IList我可以使用索引并且x => X .AdminSummaries [index] .Status可以为每一行获取正确的状态,并且还有一个索引号,可以用来更改每行的项目标识。

现在我有一个问题。我想同样的代码方法应用到这是在我与类似领域AdminSummaries(包括状态字段)模型一类的情况如下:

public ICollection<Item> Items { get; set; } 

因为它是一个ICollection的,我想我不能访问使用的元素[索引]

我可以使用foreach但如何可以提供状态值DropDownListFor的第一个参数?我怎样才能得到一个索引值,所以我可以索引行中的字段。

更新

的指数点位是我添加到原来的问题。重要的是,该解决方案可以做以下事情,以便以后可以在表单字段上使用jQuery。

id="[email protected](index)" 

回答

2

您可以使用编辑器模板。所以,你用下面的替换for循环:

@Html.EditorFor(x => x.Items) 

,然后你定义将自动为集合中的每个元素(~/Views/Shared/EditorTemplates/Item.cshtml)呈现的编辑模板:

@model Item 
<div class="rep_td0"> 
    @Html.DropDownListFor(
     x => x.SomePropertyOfItemToBindTo, 
     new SelectList(
      AdminStatusReference.GetAdminStatusOptions(), 
      "Value", 
      "Text", 
      Model.SomePropertyOfItemToBindTo 
     ) 
    ) 
</div> 
+0

您的解决方案看起来很干净确实。就一件事。我还需要能够访问计数,因为有些项目使用计数。例如:

@Model.AdminSummaries[index].RowKey
我会试着在问题中解释这一点,因为我在这里无法很好地展示它。 – 2012-01-01 13:44:24

+0

我在原始问题中添加了一些内容。不知道我是否应该为此打开一个新问题。请让我知道。我想要公平。 – 2012-01-01 13:55:38

+0

@ Samantha2,你以后需要在这些字段上用jQuery做些什么?可能有一个更简单的方法。 – 2012-01-01 14:44:37

相关问题