0

Hi there i am trying to pass multiple models into my MVC View. I Have created a View Model Class and i am getting data from my database in the GetDeviceSpecificationData() method and passing it on into the View in the Action Result GetDeviceSpecification() I am getting an error in the following statement.

@foreach (var spec in Model.myDeviceSpecifications)

The error is object reference is not set to an instance of an object. An exception of type 'System.NullReferenceException' occurred in App_Web_eervchlw.dll but was not handled in user code

My ViewModelClass

public class InventoryViewModel
{
    public List<DeviceSpecifications> myDeviceSpecifications { get; set;}
}

My Method that is getting me Device Specifications Data from my database

public List<DeviceSpecifications> GetDeviceSpecificationData()
    {
        var myDeviceSpecifications = db.DeviceSpecifications.Include(d => d.Specification).Include(d => d.Value).Include(d => d.DSID).Include(d => d.SpecID).Include(d => d.DeviceID);
        return myDeviceSpecifications.ToList();
    }

My Action Result

public ActionResult GetDeviceSpecification()
{
    InventoryViewModel mymodel = new InventoryViewModel();
    mymodel.myDeviceSpecifications = GetDeviceSpecificationData();
    return View(mymodel);
}

and My View

div class="form-group">
        @Html.LabelFor(model => model.myDeviceSpecifications, "Specification", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <select id="devspec">
                @foreach (var spec in Model.myDeviceSpecifications)
                {
                    <option value="@spec.SpecID">@spec.Specification</option>
                }

            </select>
        </div>
    </div>
Talha Malik
  • 65
  • 1
  • 8
  • 1
    Don't use foreach, imho it's better to use @Html.DropDownListFor, of course if you only need dropdown... – j.v. Mar 18 '16 at 07:46
  • Is this happening when you initially display the view, or only when you submit the form? –  Mar 18 '16 at 07:48
  • Then it gives me this error There is no ViewData item of type 'IEnumerable' that has the key “SpecID” – Talha Malik Mar 18 '16 at 07:48
  • @StephenMuecke it happens when i initially display the View – Talha Malik Mar 18 '16 at 07:49
  • Which means that `myDeviceSpecifications` is `null`. Are you sure that the code in your `GetDeviceSpecificationData()` method is correct? (and the error in you previous comment also confirms that its `null`) –  Mar 18 '16 at 07:50
  • 2
    And what on earth are all those `.Include()` statements in your query? –  Mar 18 '16 at 07:52
  • You should get null reference error on `return myDeviceSpecifications.ToList();` only. Please verify your code once again. If it is not throwing null reference error then the code line which you specified also wont throw exception. – arpan desai Mar 18 '16 at 07:54
  • @StephenMuecke I have changes the GetDeviceSpecificationData() and tried tracking it with breakpoint. but it doesn'y go to the break point . it shows me the exception i Mentioned above. As per the '.include()' is concerned its just something i saw in the actions created by scaffolding – Talha Malik Mar 18 '16 at 07:57
  • Have you checked the DeviceSpecification class? Does it also includes lists( i mean look at all the weired includes )? Also i doesn't even reaches the GetDeviceSpecificationData() ? – Rusty Mar 18 '16 at 07:57
  • Start by just using `var myDeviceSpecifications = db.DeviceSpecifications;` (remove all those `.Include()` statements). And your ` –  Mar 18 '16 at 07:59
  • @StephenMuecke I am trying to display a dropdown and i want to populate the entries from the database. I tried removing all the `.Include()` and still i am getting the same error – Talha Malik Mar 18 '16 at 08:04
  • Share the Specification class – Rusty Mar 18 '16 at 08:04
  • 1
    You have to debug your code and see why its `null` –  Mar 18 '16 at 08:05
  • public class DeviceSpecifications { [Key] public int DSID { get; set; } [Display(Name = "Device Name")] public virtual int DeviceID { get; set; } [ForeignKey("DeviceID")] public Device Device { get; set; } public virtual int SpecID { get; set; } [ForeignKey("SpecID")] public Specifications Specification { get; set; } public string Value { get; set; }} – Talha Malik Mar 18 '16 at 08:06
  • And I also suggest you read the answers [here](http://stackoverflow.com/questions/36077637/required-data-not-displayed-in-html-dropdownlist-mvc/36077768#36077768) to understand how to generate a dropdownlist –  Mar 18 '16 at 08:09
  • @TalhaMalik if the database returns null, then your myDeviceSpecifications list will remain uninitialized. That is the only issue I can understand. Have you done what i answered? – Rusty Mar 18 '16 at 08:13
  • Also as Stephen said, you gotta debug the code. – Rusty Mar 18 '16 at 08:16

1 Answers1

0

In your InventoryViewModel Class, you are declaring a list but not initializing it.
The solution is to create a constructor and inilialize the list

public class InventoryViewModel
{
   public List<DeviceSpecifications> myDeviceSpecifications { get; set;}
   public InventoryViewModel()
   {
      myDeviceSpecifications = new List< DeviceSepecifications >();
   }
}

Whenever you have a list, always follow this method.

Rusty
  • 4,138
  • 3
  • 37
  • 45
  • There might be typos but you get the idea. – Rusty Mar 18 '16 at 07:43
  • 1
    OP's line of code `mymodel.myDeviceSpecifications = GetDeviceSpecificationData();` is assigning a collection to the property! –  Mar 18 '16 at 07:47
  • Oh yeap, i did not see that – Rusty Mar 18 '16 at 07:48
  • Yeah @StephenMuecke is right here i am assigning the property a value later in my action result – Talha Malik Mar 18 '16 at 07:50
  • @Rusty I've tried creating a constructor and also i have checked that there are entries in the database. but still when i render my View it points me to the `foreach` statement and asks me to deal with it – Talha Malik Mar 18 '16 at 08:32