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>