I'm experiencing the error NullReferenceException was unhandled by user code followed by Object reference not set to an instance of an object. I've read a number of questions including the very detailed answer here but can't understand why.
The line is occurring on the line vm.HomeView.ComputerName = Environment.MachineName;
When the error occurs, Environemnt.MachineName is correctly showing the machines name as expected so there is definitely a string variable trying to be assigned.
Could anyone help explain why.
Controller:
[HttpGet]
public ActionResult Index()
{
InventoryLogViewModel vm = new InventoryLogViewModel();
vm.HomeView.ComputerName = Environment.MachineName;
vm.HomeView.RACFID = Environment.UserName;
vm.AssetTypes = (from at in db.AssetTypes
orderby at.Name
select new AssetType
{
AssetTypeId = at.AssetTypeId,
Name = at.Name
}).ToList();
return View(vm);
ViewModel:
namespace ControlsTeam.Models
{
public class InventoryLogViewModel : _LayoutViewModel
{
public IEnumerable<Department> Departments { get; set; }
public IEnumerable<AssetType> AssetTypes { get; set; }
public Inventory Inventory { get; set; }
public HomeView HomeView { get; set; }
}
public class HomeView
{
public string RACFID { get; set; }
public string ComputerName { get; set; }
}
}