我尝试使用MetadatType模型,但收到错误:
.edmx model / order.cs:
public partial class ORDER
{
public int Order_Number { get; set; }
public string Order_Type { get; set; }
}
metadatatype模型:
MetadataType(typeof(ORDERMetadata))]
public partial class ORDER
{
// Note this class has nothing in it. It's just here to add the class-level attribute.
}
public class ORDERMetadata
{
[Display(Name = "Order Number")]
public Nullable<int> Order_Number { get; set; }
[Display(Name = "Order Type")]
public string Order_Type { get; set; }
}
控制器:
List<ORDERMetadata> result = db.ORDERS.
Where(p => p.Order_Number == id).
Select(x => new ORDERMetadata
{
Order_Number = x.Order_Number,
Order_Type = x.Order_Type
}).ToList();
错误:
传递到字典中的模型项的类型为'System.Collections.Generic.List`1 [Mvc5_Whitby.Models.ORDERMetadata]',但此字典需要类型为“Mvc5_Whitby.Models.ORDERMetadata”的模型项
答案 0 :(得分:1)
在视图的顶部,你可能有这个:
@model Mvc5_Whitby.Models.ORDERMetadata
当你可能想要这个时,要匹配你传递给视图的内容:
@model IEnumerable<Mvc5_Whitby.Models.ORDERMetadata>
答案 1 :(得分:0)
您尚未发布视图,但必须
@model Mvc5_Whitby.Models.ORDERMetadata
但是你传递了一个集合(查询的结果)。更改查询以返回单个项目(例如.FirstOrDefault()
,而不是.List()
)或将视图更改为
@model List<Mvc5_Whitby.Models.ORDERMetadata>
取决于你想要的东西