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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:33:52+00:00 2026-05-14T19:33:52+00:00

I pass a ViewModel (which contains a Person object) from the EditPerson controller action

  • 0

I pass a ViewModel (which contains a “Person” object) from the “EditPerson” controller action into the view. When posted back from the view, the ActionResult receives all of the Person properties except the ID (which it says is zero instead of say its real integer)

Can anyone tell me why?

The controllers look like this:

        public ActionResult EditPerson(int personID)
    {          
        var personToEdit = repository.GetPerson(personID);

        FormationViewModel vm = new FormationViewModel();

        vm.Person = personToEdit;

        return View(vm);
    }

    [HttpPost]
    public ActionResult EditPerson(FormationViewModel model) <<Passes in all properties except ID
    {
      // Persistence code
    }

The View looks like this:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Afp.Models.Formation.FormationViewModel>" %>
    <fieldset>
        <legend>Fields</legend>


        <div class="editor-label">
            <%= Html.LabelFor(model => model.Person.Title) %>
        </div>

        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.Person.Title) %>
            <%= Html.ValidationMessageFor(model => model.Person.Title) %>
        </div>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.Person.Forename)%>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.Person.Forename)%>
            <%= Html.ValidationMessageFor(model => model.Person.Forename)%>
        </div>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.Person.Surname)%>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.Person.Surname)%>
            <%= Html.ValidationMessageFor(model => model.Person.Surname)%>
        </div>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.DOB) %>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.DOB, String.Format("{0:g}", Model.DOB))
            <%= Html.ValidationMessageFor(model => model.DOB) %>
        </div>--%>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.Person.Nationality)%>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.Person.Nationality)%>
            <%= Html.ValidationMessageFor(model => model.Person.Nationality)%>
        </div>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.Person.Occupation)%>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.Person.Occupation)%>
            <%= Html.ValidationMessageFor(model => model.Person.Occupation)%>
        </div>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.Person.CountryOfResidence)%>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.Person.CountryOfResidence)%>
            <%= Html.ValidationMessageFor(model => model.Person.CountryOfResidence)%>
        </div>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.Person.PreviousNameForename)%>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.Person.PreviousNameForename)%>
            <%= Html.ValidationMessageFor(model => model.Person.PreviousNameForename)%>
        </div>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.Person.PreviousSurname)%>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.Person.PreviousSurname)%>
            <%= Html.ValidationMessageFor(model => model.Person.PreviousSurname)%>
        </div>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.Person.Email)%>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.Person.Email)%>
            <%= Html.ValidationMessageFor(model => model.Person.Email)%>
        </div>

        <p>

            <input type="submit" value="Save" />

        </p>
    </fieldset>

And the Person class looks like:

[MetadataType(typeof(Person_Validation))]
public partial class Person
{
    public Person()
    {
    }
}

[Bind(Exclude = "ID")]
public class Person_Validation
{
    public int ID { get; private set; }
    public string Title { get; set; }
    public string Forename { get; set; }
    public string Surname { get; set; }
    public System.DateTime DOB { get; set; }
    public string Nationality { get; set; }
    public string Occupation { get; set; }
    public string CountryOfResidence { get; set; }
    public string PreviousNameForename { get; set; }
    public string PreviousSurname { get; set; }
    public string Email { get; set; }
}

And ViewModel:

public class FormationViewModel
{

    public Company Company { get; set; }

    public Address RegisteredAddress { get; set; }

    public Person Person { get; set; }

    public PersonType PersonType { get; set; }

   public int CurrentStep { get; set; }
}

}

  • 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-14T19:33:54+00:00Added an answer on May 14, 2026 at 7:33 pm

    Remove

    [Bind(Exclude = "ID")] 
    

    from you model class.

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

Sidebar

Related Questions

I have a view that I pass a viewmodel object to that contains an
I am using MVC. I have a view model which contains a Business object.
I am about to create a ViewModel to pass some data to a View.
I was wondering if you can pass a Frame which contains a control as
In a view I am adding an object of type person (for simplicity). However,
I have a viewmodel which is returned from a insert call on a MVC
I'm trying to pass my View an instance of the following ViewModel: public class
I want to pass the current DataContext (which is an instance of a ViewModel)
Is it possible to pass a function/callback from javascript to a java applet? For
I have a main page which uses a ViewModel I have created: public class

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.