NullReferenceException是未处理的asp.net实体框架

时间:2022-01-26 03:36:24

I'm new to asp.net mvc and I'm doing this exercise for myself. I created an edmx from Northwind Database. I created a controller:

我是asp.net mvc的新手,我正在为自己做这个练习。我从Northwind数据库创建了一个edmx。我创建了一个控制器:

 public ActionResult Index(int id)
    {
        var model = new IndexViewModel();
        using (var db = new ProductDB())
        {
            model.Products = from p in db.Products
                             orderby p.ProductName

                             select new IndexViewModel.InfoProduct
                             {
                                 ProductName = p.ProductName,
                                 QuantityPerUnit = p.QuantityPerUnit,
                                 UnitPrice = p.UnitPrice
                             };
        }
        return View();
    }

...the view:

    @model aspTest.Models.IndexViewModel
@{
    Layout = null;
}

...

    <div> <ul>
        @foreach (var p in Model.Products){
    <li>@p.ProductName</li>
}
        </ul>
    </div>

...and the ViewModel:

...和ViewModel:

public class IndexViewModel
{
    public IEnumerable<InfoProduct> Products { get; set; }

    public class InfoProduct
    {            
        public string ProductName { get; set; }
    }

}

But this error keeps on appearing in this part: @foreach (var p in Model.Products){

但是这个错误一直出现在这个部分:@foreach(Model.Products中的var p){

  • @p.ProductName
  • Sorry, I know this might be noobish to most of you.

    对不起,我知道这对大多数人来说可能是愚蠢的。

    2 个解决方案

    #1


    5  

    You are declaring in your model that it will work with the IndexViewModel class as:

    您在模型中声明它将与IndexViewModel类一起使用:

    @model aspTest.Models.IndexViewModel
    

    but you are sending nothing to the View from Controller as :

    但是您没有向Controller发送任何视图,因为:

    return View();
    

    Try using:

    return View(model);
    

    The error is quite true, you are trying to iterate on properties (Products) of an object (IndexViewModel) which you don't provide.

    错误是完全正确的,您正在尝试迭代您未提供的对象(IndexViewModel)的属性(Products)。

    #2


    0  

    Besides the fact that you return no ViewModel to your view and shout use return View(model); there is something else fishy with your code:

    除了你没有返回ViewModel到你的视图和shout使用return View(model)的事实;您的代码还有其他可疑之处:

    public class InfoProduct
    {            
        public string ProductName { get; set; }
    }
    

    Your class InfoProduct only contains a Productname, yet you also assign a Quantity and Price property in your select clause, this won't compile.

    您的InfoProduct类只包含Productname,但您还在select子句中指定了Quantity和Price属性,这将无法编译。

    model.Products = from p in db.Products
                     orderby p.ProductName
                     select new IndexViewModel.InfoProduct
                     {
                          ProductName = p.ProductName,
                     };
    

    Lastly, materialize your data using .ToList()

    最后,使用.ToList()实现数据

    public ActionResult Index(int id)
    {
        var model = new IndexViewModel();
        using (var db = new ProductDB())
        {
           model.Products = (from p in db.Products
                             orderby p.ProductName
                             select new IndexViewModel.InfoProduct
                             {
                                 ProductName = p.ProductName,
                             }).ToList();
           return View(model);
        }
    
    }
    

    #1


    5  

    You are declaring in your model that it will work with the IndexViewModel class as:

    您在模型中声明它将与IndexViewModel类一起使用:

    @model aspTest.Models.IndexViewModel
    

    but you are sending nothing to the View from Controller as :

    但是您没有向Controller发送任何视图,因为:

    return View();
    

    Try using:

    return View(model);
    

    The error is quite true, you are trying to iterate on properties (Products) of an object (IndexViewModel) which you don't provide.

    错误是完全正确的,您正在尝试迭代您未提供的对象(IndexViewModel)的属性(Products)。

    #2


    0  

    Besides the fact that you return no ViewModel to your view and shout use return View(model); there is something else fishy with your code:

    除了你没有返回ViewModel到你的视图和shout使用return View(model)的事实;您的代码还有其他可疑之处:

    public class InfoProduct
    {            
        public string ProductName { get; set; }
    }
    

    Your class InfoProduct only contains a Productname, yet you also assign a Quantity and Price property in your select clause, this won't compile.

    您的InfoProduct类只包含Productname,但您还在select子句中指定了Quantity和Price属性,这将无法编译。

    model.Products = from p in db.Products
                     orderby p.ProductName
                     select new IndexViewModel.InfoProduct
                     {
                          ProductName = p.ProductName,
                     };
    

    Lastly, materialize your data using .ToList()

    最后,使用.ToList()实现数据

    public ActionResult Index(int id)
    {
        var model = new IndexViewModel();
        using (var db = new ProductDB())
        {
           model.Products = (from p in db.Products
                             orderby p.ProductName
                             select new IndexViewModel.InfoProduct
                             {
                                 ProductName = p.ProductName,
                             }).ToList();
           return View(model);
        }
    
    }