ASP.Net MVC 2 is it possible to get the same instance of model(with slight changes) in HttpPost meth

Posted by jjjjj on Stack Overflow See other posts from Stack Overflow or by jjjjj
Published on 2010-05-05T14:30:18Z Indexed on 2010/05/05 14:38 UTC
Read the original article Hit count: 206

Hi

I have a complex entity User:

    public class User : BaseEntity
    {        
    public virtual Taxi Taxi { get; set; }  --> That is why i call it "complex"
    public virtual string Login { get; set; }           
    public virtual string Password { get; set; }  
    }

where Taxi is a parent of User (Taxi has-many Users):

    public class Taxi : BaseEntity
    {
      public virtual string Name { get;  set; }
      public virtual string ClientIp { get;  set; }
    }

BaseEntity consists of public virtual int Id { get; private set; }

The problem occurs while trying to edit User

    [Authorize]  
    public ActionResult ChangeAccountInfo()
    {
        var user = UserRepository.GetUser(User.Identity.Name);
        return View(user); 
    }

My ChangeAccountInfo.aspx

        <fieldset>
        <legend>Fields</legend>
        <%  %>
        <div class="editor-label">
            <%: Html.LabelFor(model => model.Login) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.Login) %>
            <%: Html.ValidationMessageFor(model => model.Login) %>      
        </div>

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

         <div class="editor-field">
            <%: Html.HiddenFor(model => model.Taxi.Name)%>               
        </div>     

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

Post changes:

    [Authorize]
    [HttpPost]
    public ActionResult ChangeAccountInfo(User model)
    {
        if (ModelState.IsValid)
        {
            UserRepository.UpdateUser(model); 

            return RedirectToAction("ChangeAccountInfoSuccess", "Account");
        }

        return View(model);
    }

But, the (User model) parameter has User.Id == 0 --> User entity had 5 before edit
User.Login == "my new login"
User.Password == "my new password"
User.Taxi.Id == 0 --> User.Taxi entity had 3 before edit
User.Taxi.Name == "old hidden name"
User.Taxi.ClientIp == null --> User entity had 192.168.0.1 before edit

Q: Is it possible not to mark all the fields of an entity (that should be in my UpdateUser) with tag "hidden" but still have them unchanged in my HttpPost method? e.g. not User.Taxi.ClientIp = null, but User.Taxi.ClientIp = 192.168.0.1

I'm using nhibernate, if it matters.

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about asp.net-mvc-2