Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9020169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:57:00+00:00 2026-06-16T04:57:00+00:00

I need some help, I have been searching everywhere for ages to why this

  • 0

I need some help, I have been searching everywhere for ages to why this keeps happening. I basically have 2 tables, Company and Address. A company can have multiple addresses. My classes and tables look like the following

TABLES:

create table Company (
    companyid int PRIMARY KEY IDENTITY not null,
    name varchar(100),
    agencyref varchar(20),
    website varchar(100),
    phoneno varchar(20),
    faxno varchar(20),
    modified datetime DEFAULT (GETDATE())   
);

create table Address (
      addressid int PRIMARY KEY IDENTITY not null,
      companyid int null, //Does the same if set to not null
        addressLine1 varchar(100),
        addressLine2 varchar(100),
        city varchar(50),
        county varchar(100),
        postcode varchar(10),
        modified datetime DEFAULT (GETDATE()),
        foreign key ( companyid ) references Company (companyid)
);

CLASSSES

public class Company
{
    [Key]
    [HiddenInput(DisplayValue = false)]
    public int companyid { get; set; }

    [DisplayName("Company Name:")]
    public string name { get; set; }

    [DisplayName("Client Reference:")]
    public string agencyref { get; set; }

    [DisplayName("Website Address:")]
    public string website { get; set; }

    [DisplayName("Phone No:")]
    public string phoneno { get; set; }

    [DisplayName("Fax No:")]
    public string faxno { get; set; }

    private DateTime _modified = DateTime.Now;
    [DataType(DataType.DateTime)]
    public DateTime modified
    {
        get { return _modified; }
        set { _modified = value; }
    }
    public virtual ICollection<Address> Address { get; set; }
}

public class Address
{
    [Key]
    [HiddenInput(DisplayValue = false)]
    public int addressid { get; set; }

    [DisplayName("Address Line 1:")]
    public string addressline1 { get; set; }

    [DisplayName("Address Line 2:")]
    public string addressline2 { get; set; }

    [DisplayName("City:")]
    public string city { get; set; }

    [DisplayName("County:")]
    public string county { get; set; }

    [DisplayName("Postcode:")]
    public string postcode { get; set; }

    private DateTime _modified = DateTime.Now;

    [DataType(DataType.DateTime)]
    public DateTime modified
    {
        get { return _modified; }
        set { _modified = value; }
    }
    public virtual Company Company { get; set; }
}

My MODEL BUILDER

modelBuilder.Entity<Company>().HasMany(c => c.Address).WithOptional(p => p.Company).Map(c => c.MapKey("companyid"));

I have been using Code First and the database is already there. the results I get from the database is:

COMPANY TABLE:

companyid   name    agencyref   website phoneno faxno   modified
4   erg NULL    NULL    NULL    NULL    2012-12-20 13:20:56.430

ADDRESS TABLE:

addressid   companyid   addressLine1    addressLine2    city    county  postcode    modified
2   NULL    hkjh    NULL    NULL    NULL    NULL    2012-12-20 13:50:37.977

As you can see I expect the companyid in Address Table to be populated with 4, but for some reason this is not doing that.

Wonder if anyone can help me solve this frustrating problem. I thought it was simple to use a view attached to a view model, that returned two classes populated with data, then insert this into the database:

My clientModel class is nice and simple:

public class ClientModel
{
    public Company Company { get; set; }
    public Address Address { get; set; }
}

MY VIEW on the controller is:

    [HttpPost]
    public ActionResult Edit(ClientModel client)
    {
       // CompanyAddress ca = new CompanyAddress();
        Domain.Data.EFDbContext context = new Domain.Data.EFDbContext();
        if (ModelState.IsValid)
        {
            context.Companies.Add(client.Company);
            context.Addresses.Add(client.Address);
            context.SaveChanges();
            return RedirectToAction("Index");
        }
        else
        {
            return View(client);
        }
    }

This is so my Edit page contains

@model TimesheetMVC.WebUI.Models.ClientModel

(enclosed within a FormMethod.Post)

<fieldset>
    <legend>Company Details</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.Company.name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Company.name)
        @Html.ValidationMessageFor(model => model.Company.name)
    </div>        
    <div class="editor-label">
        @Html.LabelFor(model => model.Company.agencyref)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Company.agencyref)
        @Html.ValidationMessageFor(model => model.Company.agencyref)
    </div>

</fieldset>
<fieldset>
    <legend>Address Details</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.Address.addressline1)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Address.addressline1)
        @Html.ValidationMessageFor(model => model.Address.addressline1)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.Address.addressline2)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Address.addressline2)
        @Html.ValidationMessageFor(model => model.Address.addressline2)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.Address.city)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Address.city)
        @Html.ValidationMessageFor(model => model.Address.city)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.Address.county)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Address.county)
        @Html.ValidationMessageFor(model => model.Address.county)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.Address.postcode)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Address.postcode)
        @Html.ValidationMessageFor(model => model.Address.postcode)
    </div>

Any help please……

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:57:01+00:00Added an answer on June 16, 2026 at 4:57 am

    You may be saving your data incorrectly. Have you tried adding your address data to your company model, then saving just the company:

    Domain.Data.EFDbContext context = new Domain.Data.EFDbContext();
    if (ModelState.IsValid)
    {
        client.Company.Address.Add(client.Address);
        context.Companies.Add(client.Company);
        context.SaveChanges();
        return RedirectToAction("Index");
    }
    else
    {
        return View(client);
    }
    

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I really need some help with this as I have been trying to fix
I need some help, i have been searching around and cant find the correct
I have been wrestling for this for a while and I need some help.
I need some help. I have been searching for days trying to find solution
I need some help with a work project I have been assigned. At the
I have been trying out Cassandra and need some help in understanding a few
Need some help to solve this. I have a gridview and inside the gridview
Need some help with a query.. I have three tables. Source id name 1
I am new to Jquery and need some help: I have a div, I
I'm a noob with mysql and php, and need some help :) I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.