Asp.net mvc 模板只能与字段访问表达式、访问属性、维度数组索引或参数自定义索引器一起使用

Asp.net mvc 模板只能与字段访问表达式、访问属性、维度数组索引或参数自定义索引器一起使用,asp.net-mvc,entity-framework,Asp.net Mvc,Entity Framework,我有这个模型: public class Proyecto { #region Atributos [DisplayName("Código")] public string ProyectoID { get; set; } public string OportunidadID {get;set;} [DisplayName("Nombre")] public string Nombre { get; set; } [DisplayN

我有这个模型:

public class Proyecto
{
    #region Atributos
    [DisplayName("Código")]
    public string ProyectoID { get; set; }

    public string OportunidadID {get;set;}
    [DisplayName("Nombre")]
    public string Nombre { get; set; }

    [DisplayName("Fecha inicio")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:d}")]
    public DateTime FechaInicio { get; set; }

    public string CodigoCliente { get; set; }

    public string TipoClienteTelco { get; set; }

    public string Sector { get; set; }

    public string AmbitoProyecto { get; set; }

    public string DescripcionServicio { get; set; }

    public string TipoServicio { get; set; }

    public DateTime CierreOperativo { get; set; }

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:d}")]
    public DateTime FechaCierreTeorica { get; set; }

    [DataType(DataType.Date)]
    [DisplayName("Fecha fin")]
    [DisplayFormat(DataFormatString = "{0:d}")]
    public DateTime FechaCierreReal { get; set; }

    public string EnquestaCalidad { get; set; }

    [DisplayName("Estado")]
    public Valoracion Estado { get; set; }

    [DisplayName("Tendencia")]
    public Valoracion Tendencia { get; set; }

    public virtual Oportunidad Oportunidad { get; set; }

    public virtual ICollection<AsientoProyecto> Asientos { get; set; }
    public virtual ICollection<IngresoProyecto> Ingresos { get; set; }
    public virtual ICollection<AsignacionProyecto> Asignaciones { get; set; }
    public virtual ICollection<Tag> Tags { get; set; }
    public virtual ICollection<Comentario> Comentarios { get; set; }
    #endregion
}
公共类项目
{
#心房区
[显示名称(“Código”)]
公共字符串ProyectoID{get;set;}
公共字符串oportunidaddid{get;set;}
[显示名称(“名称”)]
公共字符串Nombre{get;set;}
[显示名称(“Fecha inicio”)]
[数据类型(DataType.Date)]
[DisplayFormat(DataFormatString=“{0:d}”)]
公共日期时间FechaInicio{get;set;}
公共字符串codigoclient{get;set;}
公共字符串tipoclienttelco{get;set;}
公共字符串扇区{get;set;}
公共字符串AmbitoProyecto{get;set;}
公共字符串DescriptionService{get;set;}
公共字符串TipoServicio{get;set;}
公共日期时间CierreOperativo{get;set;}
[数据类型(DataType.Date)]
[DisplayFormat(DataFormatString=“{0:d}”)]
公共日期时间FechaCierreTeorica{get;set;}
[数据类型(DataType.Date)]
[显示名称(“Fecha fin”)]
[DisplayFormat(DataFormatString=“{0:d}”)]
公共日期时间fechacierrerereal{get;set;}
公共字符串EnquestaCalidad{get;set;}
[显示名称(“Estado”)]
公共Valoracion Estado{get;set;}
[显示名称(“趋势”)]
公共Valoracion Tendencia{get;set;}
公共虚拟Oportunidad Oportunidad{get;set;}
公共虚拟ICollection Asientos{get;set;}
公共虚拟ICollection入口{get;set;}
公共虚拟ICollection Asignaciones{get;set;}
公共虚拟ICollection标记{get;set;}
公共虚拟ICollection Comentarios{get;set;}
#端区
}
在我看来,我尝试插入一个@Html.editor来使用这个sintax:

<tr>
    <th>Ingresos</th>
     @foreach (var item in Model.Ingresos)
     {
         if (item.Fecha.Month < DateTime.Now.Month && item.Fecha.Year <= DateTime.Now.Year)
         {
             <td>item.Cantidad €</td>
         }
         else
         {
          @Html.EditorFor(model => Model.Ingresos.Where(i => i.ID == item.ID));
         }
     }
 </tr>

安格洛斯
@foreach(Model.Ingreos中的var项)
{
if(item.Fecha.Monthi.ID==item.ID));
}
}
我试图做的是添加一个输入文本框,用于更新“Proyecto”的Icollection的“入口”(我的模型是Proyecto)

但我得到了以下错误: 模板只能与字段访问表达式、访问属性、维度数组索引或参数自定义索引器一起使用

谁能解释一下我做错了什么? 谢谢大家!

您应该更换

@Html.EditorFor(model => Model.Ingresos.Where(i => i.ID == item.ID));

这将是一个好主意,也取代

@foreach (var item in Model.Ingresos)


通过这种方式,您的模型类型应该得到正确识别,问题也应该得到解决。

在EditorFor模板中,您试图对表达式求值以生成一个结果,这与模板的使用相反,因为在模板中创建一个变量以获得您期望的结果,然后传递给编辑器模板

{
    var filteredIngreso = Model.Ingresos.Where(i => i.ID == item.ID);
    @Html.EditorFor(model => filteredIngreso);
}
@foreach (IngresoProyecto item in Model.Ingresos)
{
    var filteredIngreso = Model.Ingresos.Where(i => i.ID == item.ID);
    @Html.EditorFor(model => filteredIngreso);
}