poultarp
asked on
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)
MODELS (.cs)
CONTROLLER (Where I try and bind data to the included models)
Can anyone help me with how to get around this error?
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)
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; }
}
}
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);
}
}
Can anyone help me with how to get around this error?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you very much indeed.