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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:43:20+00:00 2026-05-23T14:43:20+00:00

When i use foreign keys in the entity framework the foreign key object is

  • 0

When i use foreign keys in the entity framework the foreign key object is null when i do a POST. I am using MVC3 and EF 4.1. I have two tables, Product and Product Details. I am exposing them using the HTML helpers in a Razor view. When the GET happens, the product details are shown. But when i do a form Submit and post to the server, the Product Details collection is null. I lose all my changes.

Any ideas what i am doing wrong?

Thanks for your help!

The Code (i shortened it because it is fairly lengthy):

Database:

Table Product
{
  int Id
  varchar Name
}

Table ProductDetails
{
  int id,
  int ProductId, <- foreign key SQL 2008 to Product Table
  varchar Details
}


View:
@model WebSite.Models.Product
@{
    ViewBag.Title = "MyLifeSaverStoreInfo";
}

@Html.TextBoxFor(m => m.Product.Name)
@Html.TextBoxFor(m => m.Product.ProductDetails.FirstOrDefault().Description)

Controller:
public ActionResult EditProduct(int productId)
{
  var Product = _productRepository.GetProduct(productId);
  return View(product);
}


[HttpPost]
public ActionResult EditProduct(Product model)
{
   string name = model.Name; <- this update comes through
   string description = model.ProductDetails.FirstorDefault().Description;
}

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-05-23T14:43:20+00:00Added an answer on May 23, 2026 at 2:43 pm

    Got it!

    Instead of creating the “Product” entity on the POST EditProduct method, i use a Form Collection and then set each product detail according to that form collection and then save. I don’t understand why it doesn’t work the first way. I am manually updating the foreign reference. Maybe i am missing something?

    [HttpPost]
    public ActionResult EditProduct(int id, FormCollection formCollection)
    {
        var model = new MyLSstoreInfoViewModel{
                                      StoreCurrent = _profileRepository.GetProduct(Id)
                                              };
        var productDetails = model.Product.ProductDetails.Where(p => p.productId == id).Single();
    
    
                productDetaisl.Details = formCollection["details"];
                if (TryUpdateModel(model.StoreCurrent))
                {
    
                    _profileRepository.Save();
    
                }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to map the following schema using the Entity Framework. A Customer can have
I have an app that is saving to a db (using Entity Framework) and
I have been using linq in combination with entity framework for a few months
I'm trying to use LinqToEntities and just noticed that there are no foreign key
I'm using the Entity Framework as my ORM for an ASP.Net (webforms) application. Suppose
I am using the Entity Framework just to create classes that can be mapped
I have a simple application using ADO.NET EntityFramework using a many-to-many relationship between two
Is there a way in Entity Framework 4 (using CTP4 and Code First if
Choosing good primary keys, candidate keys and the foreign keys that use them is
I am using Entity framework Code First CTP5 and I am trying to find

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.