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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:50:51+00:00 2026-05-16T03:50:51+00:00

i am just getting started with ASP.NET MVC and i’m using NHibernate for my

  • 0

i am just getting started with ASP.NET MVC and i’m using NHibernate for my data context. I have kept the foreign key fields in my entity classes so that it could hopefully making working with drop down lists easier but this is not the case.

Here is my post back action:

var user = userRepository.GetById(id);

if (!TryUpdateModel(user, "User", new[] { "UserName", "RoleID" }))
    return View(user);

// Update the role
user.Role = roleRepository.GetById(user.RoleID);

This allows me to put my validation logic on the User.RoleID property.

Everything works fine until it saves it. Here’s my user and mapping class:

public virtual int UserID { get; set; }

[Required(ErrorMessage = "Username is required")]
public virtual string UserName { get; set; }

[Required(ErrorMessage = "Role is required")]
public virtual int RoleID { get; set; }

public virtual Role Role { get; set; }

public UserMap()
{
    Table("Users");
    Id(x => x.UserID);
    Map(x => x.UserName);
    Map(x => x.RoleID);
    References(x => x.Role, "RoleID");
}

However this throws back an error when it trys to commit the changes. I tried removing Map(x => x.RoleID); from the above mapping and the insert went through successfully but then the data was not populated when displaying the user.

My preferred solution would be to remove the RoleID property from the User entity (as recommended by NHibernate) but then i would have to handle my validation logic myself.

I’d appreciate it if someone could help. 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-16T03:50:52+00:00Added an answer on May 16, 2026 at 3:50 am

    i’ve found the best way to do this is to say the following:

    [HttpPost, ValidateAntiForgeryToken]
    public ActionResult Create(FormCollection collection)
    {
        var user = new User();
    
        if (!TryUpdateModel(user, new[] { "UserName", "Role" }))
            return View(user);
    
        ...
    }
    
    [HttpPost, ValidateAntiForgeryToken]
    public ActionResult Edit(int id, FormCollection collection, string returnUrl)
    {
        var user = userRepository.GetById(id);
        user.Role = new Role(); // Needed to prevent another issue when saving
    
        if (!TryUpdateModel(user, new[] { "UserName", "Role" }))
            return View(user);
    
        ...
    }
    

    For now i’m relatively happy with my hacky solution but if someone knows how i can get rid of the user.Role = new Role(); in the Edit action i’d appreciate it if they could share. Thanks

    btw if you are returning multiple data to to the view when your validation fails and you need to do a get (eg to retrieve the Roles). Then you need to Rollback the changes you’ve made in the nhibernate transaction to prevent another issue. Hope this saves someone some time :).

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

Sidebar

Related Questions

Just getting started using MVC in ASP.NET, I'm going to have it so users
I'm just getting started with ASP.NET MVC. I'm going to be using JQuery on
I'm just getting started with ASP.NET MVC 2, and playing around with Validation. Let's
I'm just getting started with ASP.NET MVC, mostly by reading ScottGu's tutorial . To
I am just getting started with ASP.NET MVC and it's great! However, I don't
I am just getting started with ASP.Net MVC 3 and am confused on this
I'm just getting started with ASP.NET MVC. What is the difference between MapRoute and
I'm just getting started with ASP.NET MVC and am going through the NerdDinner tutorial.
I am just getting started with ASP.Net MVC 3 with the Facebook C# SDK.
Have just started converting an existing job tracking system into an ASP.NET MVC application.

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.