ASP.Net MVC控制器更新模型未更新

ASP.Net MVC控制器更新模型未更新,asp.net,asp.net-mvc,Asp.net,Asp.net Mvc,正在尝试让UpdateModel为我的用户工作。User类具有基本的字符串属性,如CompanyName、FirstName、LastName等,因此没有任何异常 以下是我的视图的标题: <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Public.Master" Inherits="System.Web.Mvc.ViewPage<User>" %> 以下是我的UserModelBinder(

正在尝试让UpdateModel为我的用户工作。User类具有基本的字符串属性,如CompanyName、FirstName、LastName等,因此没有任何异常

以下是我的视图的标题:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Public.Master" Inherits="System.Web.Mvc.ViewPage<User>" %>
以下是我的UserModelBinder(取出一些错误检查代码),这似乎是问题的根源:

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;
    }
}
虽然我从数据库中获取的myUser具有其所有原始值,但控制器的UpdateModel实际上从未进行任何更改。我读过人们在ViewModels和使用哪个前缀方面存在的问题,但我只是传入常规数据库对象

奇怪的是,这个用户编辑是针对我的“公共”区域的,而我已经有了一个针对管理区域的用户编辑,它允许管理员更改额外的属性。“管理员”区域的用户编辑工作正常,但用于用户编辑的“公共”区域即使代码几乎相同也不正常

更新:

这是一个自定义ModelBinding问题,通过将我的UserModelBinding更改为从DefaultModelBinder派生并添加到我的BindModel方法中:

if (bindingContext.Model != null)
            return base.BindModel(controllerContext, bindingContext);
一切似乎都正常。

试试这个

public ActionResult Edit(User thisUser)
Id可能需要来自一个隐藏的字段

此外,还需要确保字段名与用户属性名匹配

您不需要做更多的事情,因为对象中应该包含值

如果这没有帮助,请让我知道,我将删除此答案

编辑

这是我的更新方法之一

    [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");
    }
这听起来似乎解决了您的确切问题:

UpdateModel(user, "User");

因为您要绑定到的数据似乎以viewmodel名称作为前缀。

如果要作为前缀,FormCollection将包含User.FirstName和User.lastname等键。Henrik是正确的,我已经尝试过了,但没有任何效果。我认为这可能与我创建的自定义ModelBinding有关,该自定义ModelBinding可以轻松地将经过身份验证的用户的用户实例传递到我的控制器中。进一步调查…已经完成了。我对UserModelbinder的目标基本上只是将用户传入各种控制器操作,而不是IPrincipal。我不知道这为什么会影响控制器的UpdateModel。我以前为用户设置了ModelBinding。这是否会影响用户的传入方式以及UpdateModel将如何影响它?我注意到传入的用户肯定是数据库中的用户(来自我创建的ModelBinder)。griegs,您的方法似乎有效,但前提是我从global.asax中的ModelBinder列表中删除自定义UserModelBinder类。我有点喜欢能够自动传入我的用户实例,而不是IPrincipal。有人对如何用我原来的方式制作这个有什么想法吗?你的定制活页夹是做什么的?我可以给你发一个我的例子,但我怀疑它对你有帮助。现在就发代码。基本上只返回一个用户。我对UserModelbinder的目标基本上只是将用户传入各种控制器操作,而不是IPrincipal。我不知道这为什么会影响我的控制器的UpdateModel。您的自定义模型是否执行base.OnModelUpdated(控制器上下文、绑定上下文)?如果发现任何错误,它如何添加错误?
    [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");
    }
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);
        }
    }
UpdateModel(user, "User");