Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How can i get the name of category from Product Object with code first

$
0
0

Hi

Product Model:

    public class Product
    {
        public int ProductId { get; set; }


        public string Name { get; set; }

        [Range(0.01,double.MaxValue)]
        public decimal Price { get; set; }

        public string Discription { get; set; }
        public virtual Catagory Catagory { get; set; }
    }

category model :

public class Catagory
    {
        public int CatagoryId { get; set; }



        public string Name { get; set; }

        public virtual ICollection<Product> Products { get; set; }
    }

my Action :

public IActionResult List(int CatagoryId,int pageNo=1)
        {


            return View(repository.Products.Where(p => p.Catagory.CatagoryId == CatagoryId)
                .OrderBy(o => o.ProductId)
                .Skip((pageNo - 1) * pagesize)
                .Take(pagesize).);
        }

my View :

@model IQueryable<Product><div style="direction:rtl">
    @foreach (var item in Model)
    {<div class="col-xs-3 pull-right"><p>@item.Name</p><p>@item.Price</p><p>@item.Catagory.Name</p><p>@item.ProductId</p><p>@item.Discription</p></div>
    }</div>

Error In LIne :  

<p>@item.Catagory.Name</p>
NullReferenceException: Object reference not set to an instance of an object.
AspNetCore._Views_Home_List_cshtml+<ExecuteAsync>d__0.MoveNext() in List.cshtml, line 10






 


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>