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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:07:40+00:00 2026-05-23T10:07:40+00:00

When I post my model, the Id is missing : I have an NHibernate

  • 0

When I post my model, the Id is missing :

I have an NHibernate database model “Contact” :

       public class Contact : Entity<int>
        {
            public virtual string LastName { get; set; }
            public virtual string FirstName { get; set; }
        }

        public abstract class Entity<TId>
        {
            public virtual TId Id { get; protected set; }

            public override bool Equals(object obj)
            {
                return Equals(obj as Entity<TId>);
            }
        }

I

n the controller, I set the model:

    public class MyModel
    {
        public Contact Contact { get; set; }
    }

public ActionResult MyController()
{
    MyModel model = new MyModel();

    model.Contact = ... //come from DB

    return PartialView("Contact", model);
}

In the view, I do this :

@Html.HiddenFor(m => m.Contact.Id) (when I replace with a TextBoxFor, I see the correct value)

When I post the form, in the controller all the value are in the model but the Contact.Id is 0 all the time

I post like this :

var jqxhr = $.post("Controller/MyAction", $("form").serialize(),
    function (data) {
    });

In the controller :

[HttpPost]
public ActionResult MyAction(MyModel model)
{
    //model.Contact.Id equal 0 all the time
    //other value (fields) are ok.
}

Any idea ?

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-23T10:07:41+00:00Added an answer on May 23, 2026 at 10:07 am

    You need a public setter for the id property:

    public virtual TId Id { get; set; }
    

    Otherwise how do you expect the default model binder being able to set its value? Oh, and by the way that’s one of the 10^10 + 1 reasons why you should use view models in your views instead of your domain objects.

    So here’s how a more realistic MyModel would look like:

    public class MyViewModel
    {
        public string Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
    

    That’s what you should pass to a view and that’s what you should get in return. The rest is mapping between this view model and your actual domain objects. This mapping could be greatly simplified with tools like AutoMapper. This way you leave your domain as is and you tailor the view models according to the specific requirements of the given views they are designed for.

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

Sidebar

Related Questions

I have a typical, Post model: class Post< ActiveRecord::Base validates_presence_of :user_id #Line 1 validates_presence_of
I have two models related one-to-many: a Post and a Comment : class Post(models.Model):
i have this in my BlogRepository public IQueryable<Subnus.MVC.Data.Model.Post> GetPosts() { var query = from
I have a POST method declared in my controller: [AcceptVerbs(HttpVerbs.Post)] public ActionResult UpdateComments(int id,
Let's say I have a model that looks like this: public class Blog {
I have a post model and an upvote model. upvote.rb class Upvote < ActiveRecord::Base
I have a Category and a Post model, with each Post belonging to a
I have a User model, a Post model, and an Interest model. User has_many
I have Comment as a polymorphic model. It is attached to Post, Review, etc.
I have two models, Article and Post that both inherit from a base model

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.