0

I have been messing around to find a solution as on of my view which is created using add view from respective action in List Template is giving an exception of System.NullReferenceException: Object reference not set to an instance of an object.

Line 45:     </tr>
Line 46: 
Line 47: @foreach (var item in Model) {
Line 48:     <tr>
Line 49:         <td>

Here is my Controller Function

public ActionResult ViewCarpets()
{

        IEnumerable<SelectListItem> Qualities = context.Qualities.Select(c => new SelectListItem
        {
            Value = c.Quality_Id.ToString(),
            Text = c.QName
        });
        IEnumerable<SelectListItem> Suppliers = context.Suppliers.Select(c => new SelectListItem
        {
            Value = c.Supplier_Id.ToString(),
            Text = c.SName
        });
        IEnumerable<SelectListItem> Designs = context.Design.Select(c => new SelectListItem
        {
            Value = c.Design_Id.ToString(),
            Text = c.DName
        });
        ViewBag.Quality_Id = Qualities;
        ViewBag.Supplier_Id = Suppliers;
        ViewBag.Design_Id = Designs;
        return View("ViewCarpets");
}

Here is my View

@model IEnumerable<SOC.Models.Product>

@{
    ViewBag.Title = "ViewCarpets";
}

<h2>View Carpets</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Design.DName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Quality.QName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Supplier.SName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.PColor)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.PBorder_Color)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.PSKU)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.PLength)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.PWidth)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.PCost)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.IsSold)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Design.DName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Quality.QName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Supplier.SName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PColor)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PBorder_Color)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PSKU)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PLength)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PWidth)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PCost)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.IsSold)
        </td>
        <td>
            @Html.ActionLink("Edit", "EditCarpet", new { id = item.Product_Id }) |
            @Html.ActionLink("Details", "CarpetDetails", new { id = item.Product_Id }) |
            @Html.ActionLink("Delete", "DeleteCarpet", new { id = item.Product_Id })
        </td>
    </tr>
}
</table>

and Finally Model

namespace SOC.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    [Table("Product")]
    public partial class Product
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Product()
        {
            Order_Details = new HashSet<Order_Details>();
        }

        public Product(Product product)
        {
            this.Quality_Id = product.Quality_Id;
            this.Design_Id = product.Design_Id;
            this.Supplier_Id = product.Supplier_Id;
            this.PColor = product.PColor;
            this.PBorder_Color = product.PBorder_Color;
            this.PSKU = product.PSKU;
            this.PLength = product.PLength;
            this.PWidth = product.PWidth;
            this.IsSold = product.IsSold;
        }

        [Key]
        public int Product_Id { get; set; }

        [Display(Name="Quality")]
        public int Quality_Id { get; set; }

        [Display(Name = "Design")]
        public int Design_Id { get; set; }

        [Display(Name = "Supplier")]
        public int Supplier_Id { get; set; }

        [Required]
        [StringLength(20)]
        [Display(Name = "Color")]
        public string PColor { get; set; }

        [Required]
        [StringLength(20)]
        [Display(Name = "Border Color")]
        public string PBorder_Color { get; set; }

        [Required]
        [Display(Name = "Stock #")]
        public string PSKU { get; set; }

        [Display(Name = "Length")]
        public decimal PLength { get; set; }

        [Display(Name = "Width")]
        public decimal PWidth { get; set; }

        [Display(Name = "Cost")]
        public decimal PCost { get; set; }

        [Display(Name = "In Stock")]
        public bool IsSold { get; set; }

        public virtual Design Design { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Order_Details> Order_Details { get; set; }

        public virtual Quality Quality { get; set; }

        public virtual Supplier Supplier { get; set; }
    }
}
Arslan Ali
  • 33
  • 1
  • 8
  • In addition to the answers, which are correct, be aware that your code does not call the model, which is `Model` with a capital m. – Tim Mar 20 '18 at 04:49

3 Answers3

1

You are not building your model nor invoking the overload of View() that accepts a model, so your model is null.

public ActionResult ViewCarpets()
{

        IEnumerable<SelectListItem> Qualities = context.Qualities.Select(c => new SelectListItem
        {
            Value = c.Quality_Id.ToString(),
            Text = c.QName
        });
        IEnumerable<SelectListItem> Suppliers = context.Suppliers.Select(c => new SelectListItem
        {
            Value = c.Supplier_Id.ToString(),
            Text = c.SName
        });
        IEnumerable<SelectListItem> Designs = context.Design.Select(c => new SelectListItem
        {
            Value = c.Design_Id.ToString(),
            Text = c.DName
        });
        ViewBag.Quality_Id = Qualities;
        ViewBag.Supplier_Id = Suppliers;
        ViewBag.Design_Id = Designs;

        var products = GetProducts(); // whatever you need to do to get a list of products, database call etc.
        return View("ViewCarpets", model: products);
}
Dan D
  • 2,300
  • 10
  • 17
0

You don't pass model to view:

return View("ViewCarpets");

Should be:

var model = new Your_model_class(); // Product for example
return View("ViewCarpets", model);
Backs
  • 22,763
  • 4
  • 44
  • 72
0

Thank you so much for the help guys, I got a hint from @Dan D so I changed my action method like below and the problem is solved

public ActionResult ViewCarpets()
        {
            //TODO: Implement View Carpets Logic
            IEnumerable<SelectListItem> Qualities = context.Qualities.Select(c => new SelectListItem
            {
                Value = c.Quality_Id.ToString(),
                Text = c.QName
            });
            IEnumerable<SelectListItem> Suppliers = context.Suppliers.Select(c => new SelectListItem
            {
                Value = c.Supplier_Id.ToString(),
                Text = c.SName
            });
            IEnumerable<SelectListItem> Designs = context.Design.Select(c => new SelectListItem
            {
                Value = c.Design_Id.ToString(),
                Text = c.DName
            });

            var model = new Product();

            ViewBag.Quality_Id = Qualities;
            ViewBag.Supplier_Id = Suppliers;
            ViewBag.Design_Id = Designs;

            List<Product> product = context.Products.ToList();
            return View(product);
        }
Arslan Ali
  • 33
  • 1
  • 8