2010-09-22 90 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