Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC - NHibernate - DropDownLists
    text
    copied!<p>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.</p> <p>Here is my post back action:</p> <pre><code>var user = userRepository.GetById(id); if (!TryUpdateModel(user, "User", new[] { "UserName", "RoleID" })) return View(user); // Update the role user.Role = roleRepository.GetById(user.RoleID); </code></pre> <p>This allows me to put my validation logic on the User.RoleID property.</p> <p>Everything works fine until it saves it. Here's my user and mapping class:</p> <pre><code>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 =&gt; x.UserID); Map(x =&gt; x.UserName); Map(x =&gt; x.RoleID); References(x =&gt; x.Role, "RoleID"); } </code></pre> <p>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.</p> <p>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.</p> <p>I'd appreciate it if someone could help. Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload