【问题标题】:Edit multiple instances of models asp.net mvc编辑模型asp.net mvc的多个实例
【发布时间】:2014-09-09 07:59:30
【问题描述】:

我想编辑我的模型的多个(比如 4 个)实例限制:

public int RestrictionID { get; set; }
public string portefeuille { get; set; }

在我看来,我尝试这样做:

@model IEnumerable<Management.Models.Restriction>
@for ( int i= 0; i < 4; i++)
{
    @Html.EditorFor(_ => Model.[i].portefeuille)
}

但是我有一个错误,我不能对 IEnumerable 类型使用索引。

谁能帮我解决这个问题?

【问题讨论】:

  • 你的模型必须是IList,而不是IEnumerable
  • 并删除Model[i]之间的.

标签: c# asp.net asp.net-mvc


【解决方案1】:

使用ILIst 尝试下面的代码,因为在IEnumerable 中我们不能使用索引..对于使用索引你应该去IList + 删除额外的“。”在Model[i] 之间:-

 @model ILIst <Management.Models.Restriction>
 @for ( int i= 0; i < 4; i++)
{
   @Html.EditorFor(_ => Model[i].portefeuille)
 }

更多信息请看这里:-

Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.IEnumerable<>

【讨论】:

  • 谢谢你正是我想要的“。” Model 和 [i] 之间是一个错字
  • 我很高兴它帮助了你@intern :)
【解决方案2】:

您也可以使用 linq 代替索引:

for(int i = 0; i < 4; i++)
{
    @Html.EditorFor(m => m.Skip(i).Take(1).First().portefeuille)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    • 2016-08-07
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    相关资源
    最近更新 更多