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 8652243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:15:50+00:00 2026-06-12T14:15:50+00:00

I am using MVC4 and EF5 codefirst. I have a problem whereby when creating

  • 0

I am using MVC4 and EF5 codefirst. I have a problem whereby when creating a new data record with reference to a row with a foreign key, the fields of the foreign key row are set to null.

Here is a simplified scenario:

public class Person
{
  string name { get; set; }
  Color color { get; set; }
}

public class Color
{
  int id { get; set; }
  string name { get; set; }
}

The viewmodel:

public class PersonViewModel
{
    public Person Person { get; set; }
    public IEnumerable<SelectListItem> Colors { get; set; }

    public PersonViewModel()
    {
        Context db = new Context();
        Colors = db.Colors.ToList().Select(t => new SelectListItem() { Text = t.Name, Value = t.Id.ToString() });
    }
}

The controller:

public ActionResult Create()
{
    return View(new PersonViewModel);
}

[HttpPost]
public ActionResult Create(PersonViewModel personViewModel)
{
    if (ModelState.IsValid)
    {   
        db.Person.Add(personViewModel.Person);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(personViewModel);
}

and finally, the create view:

@model PersonViewModel
@using (Html.BeginForm())
{   
    @Html.ValidationSummary(true)

    @Html.LabelFor(model => model.Person.Name) 
    @Html.EditorFor(model =>  model.Person.Name)

    @Html.LabelFor(model => model.Person.Color)
    @Html.DropDownListFor(model => model.Person.Color.Id, this.Model.Colors)

    <input type="submit" value="Create" title="Create ticket" />
}

Say I have two colors in the data:
1. red
2. blue

When creating a new person through the view and selecting blue as the color, the person will be created correctly but ‘blue’ with id 2 in color table will maintain its ID (of 2) but its name will have been set to empty.

Im sure there is a binding issue occurring here? Should this be supported as standard in the framework or do I need to create my own modelbinder?

Thanks

  • 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-12T14:15:51+00:00Added an answer on June 12, 2026 at 2:15 pm

    So it turns out I wasn’t correctly following entity framework conventions. By explicitly defining the foreign key column, entity framework correctly recognises the relationship and works as I would expect. Therefore, the new data model (adding the explicit foreign key column)

    public class Person
    {
      string Name { get; set; }
    
      int ColorId { get; set; }     
      Color color { get; set; }
    }
    
    public class Color
    {
      int ColorId { get; set; }
      string name { get; set; }
    }
    

    and the updated view (binding on the new foreign key column):

    @model PersonViewModel
    @using (Html.BeginForm())
    {   
        @Html.ValidationSummary(true)
    
        @Html.LabelFor(model => model.Person.Name) 
        @Html.EditorFor(model =>  model.Person.Name)
    
        @Html.LabelFor(model => model.Person.Color)
        @Html.DropDownListFor(model => model.Person.ColorId, this.Model.Colors)
    
        <input type="submit" value="Create" title="Create ticket" />
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using EF5 codefirst in an ASP.NET MVC4 app. I have products and
I am creating a web api using asp.net mvc4 and the response output is
I have been writing an application using ASP.Net MVC4, where the majority of the
I am using mvc4 and have a telerik control grid with editing mode enabled.
Using ASP.NET MVC4 I have created a DelegatingHandler in a WebAPI project. I use
I am using the new mvc4 ajax controller template and I don't know how
I'm using ASP.NET MVC4 (EF Code-first) with WCF ADO.NET Data Service October 2011 CTP.
I am new to asp.net MVC, using MVC4 at the moment, learning by tutorials
I'm working on a new asp.net mvc4 project using Visual Studio 2011 beta and
I just upgraded my MVC4 project to .NET 4.5 and EF5 and started using

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.