Using Base Model in Shared View
-
Hi, I would like to have a Base Model and use it in my _Shared view. I followed some examples online and did the following but I was unable to make it work: _Layout view:
@model Portal.Models.BaseViewModel
Hi, @Model.FirstName
Base Model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;namespace Portal.Models
{
public class BaseViewModel
{
public String FirstName { get; set; }
}
}Base Controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Portal.Models;namespace Portal.Controllers
{
public class BaseController : Controller
{
public BaseController()
{
BaseViewModel baseModel = new BaseViewModel()
{
FirstName = "John"
};
}
}
}Dashboard Controller:
public class DashboardController : BaseController
When I run the application, I get the following error for @Model.FirstName:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
Am I missing something? Thank you for your time and consideration.
-
Hi, I would like to have a Base Model and use it in my _Shared view. I followed some examples online and did the following but I was unable to make it work: _Layout view:
@model Portal.Models.BaseViewModel
Hi, @Model.FirstName
Base Model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;namespace Portal.Models
{
public class BaseViewModel
{
public String FirstName { get; set; }
}
}Base Controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Portal.Models;namespace Portal.Controllers
{
public class BaseController : Controller
{
public BaseController()
{
BaseViewModel baseModel = new BaseViewModel()
{
FirstName = "John"
};
}
}
}Dashboard Controller:
public class DashboardController : BaseController
When I run the application, I get the following error for @Model.FirstName:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
Am I missing something? Thank you for your time and consideration.
It's probably because - 1. you do not have constructor for BaseViewModel to assign any default value to FirstName(which you can access if no other value assigned to it), you have just declared the property for get and set. 2. In any of BaseController or DashboardController you do not have property which hold data for BaseViewModel. On BaseController you have created it but not assigned to anything. Try something like below and then access "BaseController.baseModel" to pull value of FirstName
public class BaseController : Controller
{
BaseViewModel baseModel;public BaseController() { baseModel = new BaseViewModel() { FirstName = "John" }; }
}
-
It's probably because - 1. you do not have constructor for BaseViewModel to assign any default value to FirstName(which you can access if no other value assigned to it), you have just declared the property for get and set. 2. In any of BaseController or DashboardController you do not have property which hold data for BaseViewModel. On BaseController you have created it but not assigned to anything. Try something like below and then access "BaseController.baseModel" to pull value of FirstName
public class BaseController : Controller
{
BaseViewModel baseModel;public BaseController() { baseModel = new BaseViewModel() { FirstName = "John" }; }
}
Thank you for your help :)