Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Using Base Model in Shared View

Using Base Model in Shared View

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netlinqcomarchitecture
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Farhad Eft
    wrote on last edited by
    #1

    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.

    L 1 Reply Last reply
    0
    • F Farhad Eft

      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.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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"
          };
      }
      

      }

      F 1 Reply Last reply
      0
      • L Lost User

        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"
            };
        }
        

        }

        F Offline
        F Offline
        Farhad Eft
        wrote on last edited by
        #3

        Thank you for your help :)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups