1

Using .net Core, MVC, C#

I have created a single model that contains 2 separate models. Code as below:

public abstract class ViewModelBase
{
    public string Environment { get; set; }
}

public class CombinedViewModel : ViewModelBase
{
    public FirstViewModel FirstViewModel { get;set;}
    public SecondViewModel SecondViewModel { get; set; }
}

public class FirstViewModel : ViewModelBase
{
    public string FirstName{ get;set;}
    public string LastName{ get; set; }
}

public class SecondViewModel : ViewModelBase
{
    public string Mode { get;set;}  
}

Here is my MVC Controller:

public IActionResult Index(string environment, string mode)
{
    var model = new CombinedViewModel ();
    model.Environment = environment;  
    model.SecondViewModel.Mode = mode;
    return View(model);
}

What I have searched it that this is way to initialize multiple model. Not sure what I am doing wrong here but I get the below error:

An unhandled exception occurred while processing the request.

NullReferenceException: Object reference not set to an instance of an object.

model.SecondViewModel.Mode = mode;

Do I need to initialize my first and second model separately. Please note that I am still not using my "mode" in my view yet.

Community
  • 1
  • 1
aman
  • 2,940
  • 3
  • 17
  • 32
  • 2
    Is your model.SecondViewModel null ? initialize it to new and try it if that's the case – Praneet Nadkar Mar 21 '18 at 13:48
  • 2
    `Do I need to initialize my first and second model separately.` yes that's how it works. How else do you think you can access them to set their properties? – Chetan Ranpariya Mar 21 '18 at 13:50
  • 2
    In the `CombinedViewModel` try initializing your inner models like so ```public FirstViewModel FirstViewModel { get;set;} = new FirstViewModel();``` and similar for the other one – Valeklosse Mar 21 '18 at 13:52
  • Thanks I have initialized both in my combinedviewmodel and it works fine now. – aman Mar 21 '18 at 14:07

2 Answers2

2

Yes you need to initialize them "separately". Otherwise model.SecondViewModel is null and when you try to access one of its properties you'll get an exception.

You may initialize them inside the CombinedViewModel Constructor as follow :

public CombinedViewModel () // Could request parameters like default Mode / FirstName / LastName
{
    this.FirstViewModel  = new FirstViewModel();
    this.SecondViewModel = new SecondViewModel();
}
Onyx Caldin
  • 267
  • 1
  • 10
1

Yes, you have to initialize the child models before you can access them. You can do it explicitly in the controller's action method:

public IActionResult Index(string environment, string mode)
{
    var model = new CombinedViewModel ();
    model.Environment = environment;
    model.SecondViewModel = new SecondViewModel();
    model.SecondViewModel.Mode = mode;
    return View(model);
}

Another way to go is to set some default value in the constructor of the CombinedViewModel class:

public class CombinedViewModel : ViewModelBase
{
    public FirstViewModel FirstViewModel { get;set;}
    public SecondViewModel SecondViewModel { get; set; }

    public CombinedViewModel()
    {
        this.FirstViewModel = new FirstViewModel();
        this.SecondViewModel = new SecondViewModel();
    }
}
Faruq
  • 1,024
  • 8
  • 23