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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:26:46+00:00 2026-05-20T21:26:46+00:00

I have a model called Project, that has the following properties (simplified for brevity’s

  • 0

I have a model called Project, that has the following properties (simplified for brevity’s sake).

[DataContract(IsReference = true)]    
public class Project
{
    [Key]
    [DataMember]
    public int id { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public DateTime CreatedDate { get; set; }
}

The model is also a datacontract for a WCF service that uses Entity Framework 4 to query a datastore. The code for the model is generated from a template that automatically generates a CRUD service layer against the Entity Framework Model.

Now, my MVC2 application has a view containing a form to edit the fields. The controllers Edit action accepts the entire model as an argument upon POST.

[HttpPost]      
public ActionResult Edit(Project project)       
{           
    var context = new ServiceContext();             
    try             
    {
        if (ModelState.IsValid)
        {
            project = context.UpdateProject(project);
            return RedirectToAction("Index");
        }           
    }           
    catch   
    {
        ModelState.AddModelError("", "Could not save project");             
    }

    return View(project);       
}

Now, my problem is that when the form is posted to the controller, the Project model has all its fields correctly populated except for the ‘id’ property, which defaults to 0.

I’ve done some digging and pleaded with Uncle Google for answers, but the closest fix I could get was to add the following to the model’s class,

[Bind(Include="id")]

which works fine, but ONLY populates the ‘id’ property, meaning that I would have to explicitly specify each property to be included in the model binding. Obviously, this can get nasty, especially since the model itself has many more properties than the one’s I’ve shown above.

Is there any other way to get this working?

Gut feel is that the [Key] attribute has something to do with it, but I haven’t been able to figure anything out.

The form has a hidden input for the ‘id’ property.

<%: Html.HiddenFor(model => model.id)%>
  • 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-20T21:26:47+00:00Added an answer on May 20, 2026 at 9:26 pm

    I think I have found the solution.

    My model also has some complex properties which map to related tables in my entity framework model. Now, since I’m using the Self-Tracking Entities T4 templates to generate my service layer, it has some additional logic when it comes to mapping values to entities. In this case, one of the complex properties is a class called Status which looks like this:

    public class Status
    {
        public int ProjectId { get; set; }
        public int StatusId { get; set ; }
    }
    

    The ProjectId is a foreign key to the Project table in my datastore. I noticed that the ProjectId field was also set to 0 during model binding.

    When I added the following to my view

    <%: Html.HiddenFor(model => model.Status.ProjectId) %>
    

    both fields had the correct value posted to the controller.

    Problem solved 🙂

    Thanks swapneel for your thoughts on the matter.

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

Sidebar

Related Questions

In a current project, I have a model called Box that stores items from
Let's say I have a model class called Project, but instead of this: class
I have a model called Event and another called Product. An event has many
I have a model called Answer which has a ForeignKey relationship to another model
I have a model called company that has_many users then users belongs_to company. class
I have the following project structure: base project (service layer, model) web project webservice
I have a core data model with an entity called clients that is made
I have a model called Project, which is a collection of information stored by
I'm working on an MVC 2 project and I have a model that looks
I have a weird design question. I have a model called Article, which has

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.