简体   繁体   中英

Two models in one partial view on C# MVC 4.5

I am trying to add data from a partial view named GetEmailingSystem.cshtml. Here's a glimpse of what's in it:

<div class="form-double">
    <input type="text" value="@Model.FirstName" id="FirstName" name="FirstName" placeholder="Your name" class="form-control" required="required">
    <input type="text" value="@Model.LastName" id="LastName" name="LastName" placeholder="Your surname" class="form-control" required="required">
</div>

There's an Index.cshtml which that partial view binds to and its attached through the code shown below:

<div class="col-xs-12 col-md-8">
    @Html.Action("GetEmailingSystem", "Home")
</div>

My controller sits with the following code:

public ActionResult GetEmailingSystem()
{
  TelecomClientCompanyViewModel _emailSystem = new TelecomClientCompanyViewModel();
    _emailSystem.telecomClient = GetTelecomClientModel();
    _emailSystem.telecomClientCompanies = GetTelecomClientCompanyModel();
    return View(_emailSystem);
}
public TelecomClient GetTelecomClientModel()
{
    TelecomClient _teleModel = new TelecomClient();
    return _teleModel;
}
public TelecomClientCompany GetTelecomClientCompanyModel()
{
   TelecomClientCompany _telecomClientCompany = new TelecomClientCompany();
   return _telecomClientCompany;
}

TelecomClientCompanyViewModel is a ViewModel with the code below:

    public class TelecomClientCompanyViewModel
    {
         public TelecomClient telecomClient { get; set; }
         public TelecomClientCompany telecomClientCompanies { get; set; }
    }

TelecomClient is one model with this code:

public class TelecomClient
{
    public int id { get; set; }
    public string Title { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string UnitNo { get; set; }
    public string UnitStreetName { get; set; }
    public string ComplexNo { get; set; }
    public string ComplexName { get; set; }
    public string StreetName { get; set; }
    public string Suburb { get; set; }
    public string City { get; set; }
    public string Province { get; set; }
    public string Country { get; set; }
    public string CellNo { get; set; }
    public string TelephoneNo { get; set; }
    public string EmailAddress { get; set; }
    public string Website { get; set; }
    public int companyId { get; set; }
    public string PositionHeld { get; set; }
    public string OfficeLocation { get; set; }
    public DateTime DateAdded { get; set; }
    public string UpdatedBy { get; set; }
    public DateTime DateUpdated { get; set; }
    public int Active { get; set; }
}

Where TelecomClientCompany has this code in it:

public class TelecomClientCompany
{
    public int id { get; set; }
    public string CompanyName { get; set; }
    public string VATNo { get; set; }
    public string CompanyUnitNo { get; set; }
    public string CompanyUnitStreetName { get; set; }
    public string CompanyComplexNo { get; set; }
    public string CompanyComplexName { get; set; }
    public string CompanyStreetName { get; set; }
    public string CompanySuburb { get; set; }
    public string CompanyCity { get; set; }
    public string CompanyProvince { get; set; }
    public string CompanyCountry { get; set; }
    public string CompanyTelephoneNo { get; set; }
    public string NoOfEmployees { get; set; }
    public string CompanyWebsite { get; set; }
    public string CompanyIndustry { get; set; }
}

Yet, I don't know what am I missing with all this since I keep getting this error message saying "Object reference not set to an instance of an object.". I was hoping that by hard coding the data would make this code to work which is all I see in my research online. But that's not what I want, I'd like to be able to insert data through forms to the database and be able to get that data from that database. So I'm obviously using two tables and I need to insert through two models on one view. Can somebody please help me with this since I've been struggling for close to a week with this. Thanks you!

public TelecomClient GetTelecomClientModel()
{
    TelecomClient _teleModel = new TelecomClient();
    return _teleModel;
}
public TelecomClientCompany GetTelecomClientCompanyModel()
{
   TelecomClientCompany _telecomClientCompany = new TelecomClientCompany();
   return _telecomClientCompany;
}

These 2 methods will return "empty" objects for TelecomClient and TelecomClientCompany. Since you have numerous strings inside those classes, which are reference types, they will all default to null since you aren't assigning them a value anywhere that we can see in the code you provided. In your view, you are accessing properties like FirstName and LastName which are null.

Since we aren't seeing all your code, I'm guessing this is where your "Object reference not set to an instance of an object." exception is being thrown. There is nothing inherently wrong with sending a view model over that is an amalgamation of several related or unrelated objects. In fact, this is one of the reasons for having a view model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM