Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC 5错误,但此字典需要类型为';System.Collections.Generic.IEnumerable_C#_Asp.net Mvc - Fatal编程技术网

C# MVC 5错误,但此字典需要类型为';System.Collections.Generic.IEnumerable

C# MVC 5错误,但此字典需要类型为';System.Collections.Generic.IEnumerable,c#,asp.net-mvc,C#,Asp.net Mvc,我是Ninjet和Mvc的新手。我知道这个错误意味着我向视图传递了错误的类型。但我不知道哪里出了错。这是我的课 public class ProdListViewModel { public ProdListViewModel() { this.ProductsList = new List<ProdViewModel>(); } public ProdListViewModel(List<product> product

我是Ninjet和Mvc的新手。我知道这个错误意味着我向视图传递了错误的类型。但我不知道哪里出了错。这是我的课

 public class ProdListViewModel
{
    public ProdListViewModel()
    {
        this.ProductsList = new List<ProdViewModel>();
    }
    public ProdListViewModel(List<product> products)
    {
        this.ProductsList = new List<ProdViewModel>();

        foreach (var prod in products)
        {
            this.ProductsList.Add(new ProdViewModel(prod));


        }

    }

    public List<ProdViewModel> ProductsList { get; set; }
}

 public class ProdViewModel
{
    [Key]
    public int productsID { get; set; }

    [Required]
    [StringLength(50)]
    public string pname { get; set; }

    public ProdViewModel()
    {
    }

    public ProdViewModel(product product)
    {
        productsID = product.productsID;
        pname = product.pname;

    }
}
//在这里,我从存储库中获取产品,只带了2个 var product=productRepository.Products.Take(2).OrderBy(p=>p.productsID.ToList()

以下是我通过VS生成的视图

@model IEnumerable
@{
ViewBag.Title=“FrontPageList”;
}
首页列表

@ActionLink(“新建”、“创建”)

@foreach(模型中的var项目){ @ActionLink(“编辑”,“编辑”,新的{/*id=item.PrimaryKey*/})| @ActionLink(“详细信息”,“详细信息”,新的{/*id=item.PrimaryKey*/})| @ActionLink(“删除”,“删除”,新的{/*id=item.PrimaryKey*/}) }
这是完全的错误 传递到字典中的模型项的类型为“SalesRep.Domain.ViewModels.TuscanyWeb.ProdListViewModel”,但此字典需要“System.Collections.Generic.IEnumerable”类型的模型项“1[SalesRep.Domain.ViewModels.TuscanyWeb.ProdListViewModel]”

另一方面,如果在控制器I中

//List<ProdViewModel> basicObjectList =
        //     product.ConvertAll(x => new ProdViewModel
        //     {
        //         pname = x.pname,
        //          productsID = x.productsID
        //     });
//列表基本对象列表=
//product.ConvertAll(x=>新的ProdViewModel
//     {
//pname=x.pname,
//productsID=x.productsID
//     });

并将basicObjectList传递给视图i works fine

您不需要
@model IEnumerable
,只需要
@model SalesRep.Domain.ViewModels.TuscanyWeb.ProdListViewModel
。然后,在for循环中,只需这样做:

@foreach (var item in Model.ProductsList ) {

你能同时发布你的
FrontPageList.cshtml
模板吗?我用视图编辑了这个问题谢谢你,效果很好。哇!所以问题,当我在VisualStudio中从模板创建视图时,我不能选择列表,那么我应该选择什么?或者我必须手动查看?手动创建视图是最好的选择。请对此答案进行投票,并/或将其作为接受答案。
//List<ProdViewModel> basicObjectList =
        //     product.ConvertAll(x => new ProdViewModel
        //     {
        //         pname = x.pname,
        //          productsID = x.productsID
        //     });
@foreach (var item in Model.ProductsList ) {