• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 527
  • Last Modified:

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?
0
poultarp
Asked:
poultarp
1 Solution
 
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
 
poultarpAuthor Commented:
That was just what was needed!

Thank you very much indeed.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now