2010-09-22 94 views
3

试图让UpdateModel为我的用户工作。用户类具有基本的字符串属性,如公司名称,名字,姓氏等,所以没有什么特别的。ASP.Net MVC控制器UpdateModel不更新

这里是我的看法标题:

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

后,他们提出,在我的控制器,代码如下所示:

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

值近到的FormCollection有这样的价值观:

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

这是我的UserModelBinder(拿出一些错误检查代码),这似乎是t他的问题的根源:

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

一切似乎工作。

回答

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

那么,字符串ID来自编辑中的URL: – enantiomer2000 2010-09-22 23:16:43

+0

我以前已经为用户设置了一个ModelBinding。这可能会影响用户传入的方式以及UpdateModel如何影响它?我注意到被传入的用户肯定是来自数据库的用户(来自我创建的ModelBinder)。 – enantiomer2000 2010-09-22 23:23:42

+0

griegs,您的方法似乎工作,但只有当我从Global.asax的ModelBinders列表中删除自定义的UserModelBinder类。我有点喜欢能够让我的用户实例自动传入,而不是IPrincipal。任何人对如何以我的原始方式进行这项工作有任何想法? – enantiomer2000 2010-09-22 23:32:02

2

blog post听起来像它解决您的具体问题:

UpdateModel(user, "User"); 

因为它似乎要绑定到由视图模型名称为前缀的数据。

+1

如果它将被加上前缀,FormCollection将包含Keys,如User.FirstName和User.LastName – 2010-09-22 23:20:56

+0

Henrik是正确的,我已经试过这个无济于事。我认为这可能与我创建的自定义ModelBinding有关,可以轻松地将我的控制器传递给经过身份验证的用户的User实例。进一步调查... – enantiomer2000 2010-09-22 23:26:58

+0

你可以发布你的自定义ModelBinding代码? – amurra 2010-09-22 23:36:48