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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:43:13+00:00 2026-05-16T23:43:13+00:00

Trying to get UpdateModel to work for my User. The User class has basic

  • 0

Trying to get UpdateModel to work for my User. The User class has basic string properties like CompanyName, FirstName, LastName, etc so nothing exotic.

Here is the header for my view:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Public.Master" Inherits="System.Web.Mvc.ViewPage<User>" %>

After they submit, in my controller, the code looks like this:

[HttpPost]
    public ActionResult Edit(string id, FormCollection collection)
    {
        try
        {
            User myUser = db.Get<IUserRepository>().Get(id);
            UpdateModel(myUser);
            db.Update(myUser);

            return RedirectToAction("Index", "Home");
        }
        catch
        {
            return View();
        }
    }

The values past into FormCollection have the values like:

[0] "FirstName" string
[1] "LastName" string
[2] "Email" string

Here is my UserModelBinder (took out some error checking code) which seems to be the source of the problem:

public class UserModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        IPrincipal p = controllerContext.HttpContext.User;
        User u = db.Get(p.Identity.Name);
        return u;
    }
}

while the myUser I get from the database has all its original values, the UpdateModel for my controller never actually makes any changes. I have read problems people have with ViewModels and which prefix to use, but I am just passing in the regular database object.

The strange thing is that this User edit is for my “Public” area and I already have a User edit for the Admin area which lets the administrator change extra Properties. The “Admin” area User edit is working fine, but the “Public” area for User edit isn’t even though the code is almost identical.

Update:

This turned out to be a custom ModelBinding problem and by changing my UserModelBinding to derive from DefaultModelBinder and adding into my BindModel method:

if (bindingContext.Model != null)
            return base.BindModel(controllerContext, bindingContext);

Everything seems to work.

  • 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-16T23:43:14+00:00Added an answer on May 16, 2026 at 11:43 pm

    Try this instead

    public ActionResult Edit(User thisUser)
    

    Id will need to come from a hidden field maybe.

    Also you will need to ensure your field names match the User property names.

    You shouldn’t need to do anything more as the object should have the values within it.

    If this is not helpful then let me know and I’ll remove this answer

    edit

    This is one of my update methods

        [HttpPost]
        public ActionResult EditCustomer(Customer customer)
        {
            //ensure that the model is valid and return the errors back to the view if not.
            if (!ModelState.IsValid)
                return View(customer);
    
            //locate the customer in the database and update the model with the views model.
            Customer thisCustomer = customerRepository.Single(x => x.CustomerID == customer.CustomerID);
            if (TryUpdateModel<Customer>(thisCustomer))
                customerRepository.SaveAll();
            else
                return View(customer);
    
            //return to the index page if complete
            return RedirectToAction("index");
        }
    

    edit 2

    my custom model binder

    public class CustomContactUsBinder : DefaultModelBinder
        {
            protected override void OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext)
            {
                ContactFormViewModel contactFormViewModel = bindingContext.Model as ContactFormViewModel;
    
                if (!String.IsNullOrEmpty(contactFormViewModel.Name))
                    if (contactFormViewModel.Name.Length > 10)
                        bindingContext.ModelState.AddModelError("Name", "Name is too long.  Must be less than 10 characters.");
    
                base.OnModelUpdated(controllerContext, bindingContext);
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get server-side validation of an Entity Framework String Property to work.
I am trying get all html links within a string and replace them using
I am trying get all html links within a string and replace them using
I am trying get Struts 2 and Tiles to work and I am using
trying to get same string on tooltip as is in the Text e.g. Text=<%#
Trying to get this expression to work, can someone look at it and tell
Trying to get a random string out of ListArray on the button pressed. Always
Trying to get a value from a object isn't working out. class Asset <
I'm trying get LocalConnection to work between two swf's placed within two different IFRAMES.
I'm trying get online friends by user in XMPP server (Ejabberd). I'm using Ruby

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.