ASP.Net MVC Controller UpdateModel does not update

Trying to get UpdateModel to work for my user. The User class has basic string properties such as CompanyName, FirstName, LastName, etc. Therefore, nothing is exotic.

Here is the title for my presentation:

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

After sending 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();
        }
    }

Values ​​passed in FormCollection have the following meanings:

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

Here is my UserModelBinder (got 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;
    }
}

and myUser, which I get from the database, has all its original values, UpdateModel for my controller never makes any changes. I have problems reading with ViewModels and what prefix to use, but I just pass in a regular database object.

, "" , , . "" , "" , .

Update:

ModelBinding UserModelBinding DefaultModelBinder BindModel:

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

, .

+3
2

public ActionResult Edit(User thisUser)

.

, .

, .

, ,

    [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");
    }

2

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);
        }
    }
+2

, :

UpdateModel(user, "User");

, , , viewmodel.

+2

Source: https://habr.com/ru/post/1766102/


All Articles