MVC 5: initialize nested model

Hi

I have an MVC 5 webpage which uses Partial Views. The partial views on the Main View page uses different Models. I have included all the models as properties in one of the Models, but when I try to add data to the included Models, I get an error saying that the included Model is Null.

Here is my code:

MAIN VIEW (.cshtml)
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}


@model GbizzVirk_dk.Models.CompanyInfos


<p>
  På denne side kan du vedligeholde virksomhedens kontakt, login og faktureringsoplysninger.
</p>

@Html.Partial("_UpdateCompanyInfo", Model)

@Html.Partial("_UpdateContactInfo", Model.ComConInfo)

@Html.Partial("_UpdateInvoiceInfo", Model.ComInvInfo)

Open in new window


MODELS (.cs)
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GbizzVirk_dk.Models
{
  public class CompanyInfo : Controller
  {
    // GET: CompanyInfo
    public ActionResult Index()
    {
      return View();
    }
  }

  public class CompanyContactInfo
  {
    [Required]
    [Display(Name = "Kontaktperson")]
    public string ContactPerson { get; set; }

    [Required]
    [Display(Name = "Kontakttelefon nr.")]
    public string ContactPhone { get; set; }
  }

  public class CompanyLoginInfo
  {
    [Required]
    [Display(Name="Kontakt/login email")]
    public string LoginEmail {get; set;}

    [Required]
    [Display(Name = "Gentag email")]
    public string RepeatLoginEmail { get; set; }
    
    [Required]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Required]
    [Display(Name = "Gentag password")]
    public string RepeatPassword { get; set; }
  }

  public class CompanyInvoiceInfo
  {
    [Required]
    [Display(Name = "Faktura email")]
    public string AccountEmail { get; set; }

    [Required]
    [Display(Name = "Gentag faktura email")]
    public string RepeatAccountEmail { get; set; }
  }

  public class CompanyInfos
  {
    public CompanyContactInfo ComConInfo;
    public CompanyInvoiceInfo ComInvInfo;

    [Required]
    [Display(Name = "Virksomhedsnavn")]
    public string CompanyName { get; set; }

    [Display(Name = "C/O navn")]
    public string COName { get; set; }

    [Required]
    [Display(Name = "Vejnavn")]
    public string CompanyAddress { get; set; }

    [Required]
    [Display(Name = "Husnr.")]
    public string CompanyHouseNo { get; set; }

    [Required]
    [Display(Name = "Postnummer")]
    public string CompanyZipCode { get; set; }

    [Required]
    [Display(Name = "By")]
    public string CompanyCity { get; set; }

    [Required]
    [Display(Name = "Telefonnummer")]
    public string CompanyPhone { get; set; }

    [Display(Name = "Tilmeld nyhedsbrev")]
    public Boolean NewsLetter { get; set; }
  }
}

Open in new window


CONTROLLER (Where I try and bind data to the included models)
public class CompanyInfoController : Controller
  {
    //New instance of the Model with the nested models
    CompanyInfos ComInfo = new CompanyInfos();
        
    // GET: CompanyInfo
    public ActionResult ViewCompanyInfo()
    {
      using (SqlConnection con = new    SqlConnection(ConfigurationManager.ConnectionStrings["gbizz"].ConnectionString))
      {
        con.Open();
        SqlCommand sqlCom;
        SqlDataReader sqlReader;

        sqlCom = new SqlCommand("Select * From Customer Where CustomerId = @CustomerId", con);
        sqlCom.CommandType = CommandType.Text;
        sqlCom.Parameters.Add("@CustomerId", SqlDbType.Int).Value = 12;

        sqlReader = sqlCom.ExecuteReader();

        if (sqlReader.HasRows)
        {
          sqlReader.Read();
          //Tilskriver virksomhedsinformationerne til CompanyInfos Class
          ComInfo.CompanyName = sqlReader["Company"].ToString();
          ComInfo.COName = sqlReader["CoName"].ToString();
          ComInfo.CompanyAddress = sqlReader["StreetName"].ToString();
          ComInfo.CompanyHouseNo = sqlReader["StreetNo"].ToString();
          ComInfo.CompanyZipCode = sqlReader["Zipcode"].ToString();
          ComInfo.CompanyCity = sqlReader["City"].ToString();
          ComInfo.CompanyPhone = sqlReader["PhoneNo"].ToString();
          if (sqlReader["Newsletter"].ToString() == "0")
            ComInfo.NewsLetter = false;
          else
            ComInfo.NewsLetter = true;

          //THE LINES BELOW GIVES THE "NULL" ERROR
          ComInfo.ComConInfo.ContactPerson = sqlReader["ContactPerson"].ToString(); 
          ComInfo.ComConInfo.ContactPhone = sqlReader["ContactPersonPhoneNo"].ToString();
          
          ComInfo.ComInvInfo.AccountEmail = sqlReader["AccountEmail"].ToString();
          ComInfo.ComInvInfo.RepeatAccountEmail = sqlReader["AccountEmail"].ToString();          
        }
        sqlReader.Close();
        sqlReader.Dispose();
        sqlCom.Dispose();
        con.Close();
        con.Dispose();
      }
      return View(ComInfo);
    }
}

Open in new window


Can anyone help me with how to get around this error?
LVL 1
poultarpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gautham JanardhanCommented:
first i think these

 public CompanyContactInfo ComConInfo;
    public CompanyInvoiceInfo ComInvInfo;

Open in new window


needs to be properties..

second i dont see u initializing this anywhere

try this

public class CompanyInfos
  {
    public CompanyContactInfo ComConInfo{get;set;}
    public CompanyInvoiceInfo ComInvInfo{get;set;}
public CompanyInfos()
{
ComConInfo = new CompanyContactInfo ();
ComInvInfo = new CompanyInvoiceInfo ();
}
    [Required]
    [Display(Name = "Virksomhedsnavn")]
    public string CompanyName { get; set; }

    [Display(Name = "C/O navn")]
    public string COName { get; set; }

    [Required]
    [Display(Name = "Vejnavn")]
    public string CompanyAddress { get; set; }

    [Required]
    [Display(Name = "Husnr.")]
    public string CompanyHouseNo { get; set; }

    [Required]
    [Display(Name = "Postnummer")]
    public string CompanyZipCode { get; set; }

    [Required]
    [Display(Name = "By")]
    public string CompanyCity { get; set; }

    [Required]
    [Display(Name = "Telefonnummer")]
    public string CompanyPhone { get; set; }

    [Display(Name = "Tilmeld nyhedsbrev")]
    public Boolean NewsLetter { get; set; }
  }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
poultarpAuthor Commented:
That was just what was needed!

Thank you very much indeed.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.