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);
, .