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

  • Home
  • SEARCH
  • 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 5843351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:08:33+00:00 2026-05-22T12:08:33+00:00

Heres the scenario: I have a User object like this: public class User :

  • 0

Heres the scenario: I have a User object like this:

 public class User : BaseEntity<User>, IAggregateRoot
 {
    public virtual string Name { get; set; }
    public virtual string Username { get; set; }
    public virtual string Password { get; set; }
    public virtual string SecretQuestion { get; set; }
    public virtual string SecretAnswer { get; set; }
    public virtual DateTime LastLogin { get; set; }
 }

During the editing of this object, i load it into the view, but i only want to update some of the properties (ie i wouldnt want to update LastLogin property). In this situation what would i do?

Is the best strategy to create a user viewmodel, and will nhibernate cope with this when i try to update a user object with a null LastLogin field?

Thanks in advance.

EDIT

Something like this:

public class UserViewModel
{
  public string Name {get;set;}
  public string UserName {get;set;}
  public string Password {get;set;}
  public string SecretQuestion {get;set;}
  public string SecretAnswer {get;set;}
}

And then the editing:

public ActionResult Edit(int id)
{
  return View(_userRepository.FindById(id));
}

[HttpPost]
public ActionResult Edit(int id, UserViewModel userViewModel)
{
  try
  {

    //Not sure how to update the model 
    //with the view Model and save.

    _userRepository.Update(????);
    return RedirectToAction("Index");
  }
  catch
  {
    return View();
  }
}
  • 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-22T12:08:34+00:00Added an answer on May 22, 2026 at 12:08 pm

    A good approach is to create a UserViewModel with only the properties you want to display/update. Don’t let nHibernate know about the view model. Then, when the edits are posted back to your controller you retrieve the actual User object from nHibernate, update it’s properties from the view model, and then save it back to the database.

    Update

    Something like this:

    [HttpPost]
    public ActionResult Edit(int id, UserViewModel userViewModel)
    {
      try
      {
        User model = _userRepository.FindById(id);
    
        model.Name = userViewModel.Name;
        model.Username = userViewModel.Username;
        model.Password = userViewModel.Password;
        model.SecretQuestion = userViewModel.SecretQuestion;
        model.SecretAnswer = userViewModel.SecretAnswer;
    
        _userRepository.Update(model);
        return RedirectToAction("Index");
      }
      catch
      {
        return View();
      }
    }
    

    In a project I’ve been working on recently I created a ViewModelBase class which includes methods that maps properties from a domain model to a view model and back again based on matching the property name and type. All my view models are derived from ViewModelBase.

    There are other tools like AutoMapper that do this sort of thing and much, much more.

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

Sidebar

Related Questions

I have a Userdetails class like the one below public class UserDetails { public
Scenario 1: I have two classes: Post : class Post { String content Date
This is the scenario: I'm developing a dropdown widget in Jquery. I have to
Here's the scenario: I have the following user control, the idea is that it's
I have the follow problem. This is the situation i've got when user login
This is the scenario: I have a WCF service running, who communicates with this
I have a form that creates a new brand. The user chooses a name,
Here's the scenario: I have 2 tables: CREATE TABLE dbo.API_User ( id int NOT
Here's my scenario - I have an SSIS job that depends on another prior
Here's the scenario: You have a Windows server that users remotely connect to via

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.