简体   繁体   English

NullReferenceException未处理asp.net实体框架

[英]NullReferenceException was unhandled asp.net entity framework

I'm new to asp.net mvc and I'm doing this exercise for myself. 我是asp.net mvc的新手,我正在为自己做这个练习。 I created an edmx from Northwind Database. 我从Northwind数据库创建了一个edmx。 I created a controller: 我创建了一个控制器:

 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 @ p.ProductName
  • Sorry, I know this might be noobish to most of you. 抱歉,我知道这可能对你们大多数人都不友好。

    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 : 但是您没有向控制器发送任何内容给View:

    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 )。

    Besides the fact that you return no ViewModel to your view and shout use return View(model); 除了不向视图返回ViewModel并大喊大叫外,还使用return View(model); there is something else fishy with your code: 您的代码还有其他问题:

    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);
        }
    
    }
    

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM