2010-11-29 104 views
0

我有一個使用實體框架的ASP.NET MVC 2.0應用程序。我所有的觀點都使用視圖模型,其中大部分都是複雜的。含義...要編輯的對象是視圖模型的屬性,而不是視圖模型本身。ModelState.IsValid在「新」表單上工作,但不在「編輯」表單上工作

我在數據註釋中使用了部分類,並在控制器中的POST動作中檢查了ModelState.IsValid。

我有一個「新」形式和一個「編輯」窗體爲3個字段的簡單對象!

ModelState.IsValid檢查適用於新窗體,並顯示正確的「必填字段」錯誤,如果我嘗試提交空白窗體。

但如果我加載一個編輯表單,並從需要一些文本框清除值,並提交表單,我沒有得到驗證錯誤,我只是得到一個異常:

錯誤執行子請求處理程序'System.Web.Mvc.HttpHandlerUtil + ServerExecuteHttpHandlerWrapper'。

所以我的問題是,ModelState.IsValid不能用EDIT形式工作,因爲它可能是從加載的視圖模型對象而不是FormCollection中查看值?



    // this one does not validate 

     [AcceptVerbs(HttpVerbs.Post)] 
     public ActionResult Edit(int accountStatusKey, AccountStatusEditViewModel model, FormCollection values) 
     { 
      if (ModelState.IsValid) 
      { 
       db.UpdateAccountStatus(accountStatusKey, values); 
       return RedirectToAction("States"); 
      } 
      else 
      { 
       return View("Edit", model); 
      } 
     } 


    // this one does validate 

     [AcceptVerbs(HttpVerbs.Post)] 
     public ActionResult New(AccountStatusNewViewModel model, FormCollection values) 
     { 
      if (ModelState.IsValid) 
      { 
       db.AddAccountStatus(values); 

       return View("States", new AccountStatusStatesViewModel()); 
      } 
      else 
      { 
       return View("New", model); 
      } 
     } 

    // how I arrive AT the edit form 

     [AcceptVerbs(HttpVerbs.Get)] 
     public ActionResult Edit(int accountStatusKey) 
     { 
      return View("Edit", new AccountStatusEditViewModel(accountStatusKey)); 
     } 

    // and finally, the view model code 

    public class AccountStatusEditViewModel : ViewModelBase 
    { 

     public AccountStatus AccountStatus { get; private set; } 

     public IEnumerable States { get; private set; } 

     public List StatusTypes { get; private set; } 

     public AccountStatusEditViewModel(int accountStatusKey) 
     { 
      AccountStatus = db.GetAccountStatusByKey(accountStatusKey); 
      States = db.GetAllStates(); 

      StatusTypes = new List(); 
      StatusTypes.Add("Primary Status"); 
      StatusTypes.Add("Secondary Status"); 
      StatusTypes.Add("External Status"); 
     } 

     public AccountStatusEditViewModel() 
     { 
     } 

    } 

    // this action method does not work at all either - no db updating, no validation 
    // the page simply redirects to "States" view, which should only happen if the db 
    // was being updated, right? But nothing is changing in the DB, and the validation 
    // never happens. 

     [AcceptVerbs(HttpVerbs.Post)] 
     public ActionResult Edit(AccountStatusEditViewModel model) 
     { 
      if (ModelState.IsValid) 
      { 
       if (TryUpdateModel(model, "AccountStatus")) 
       { 
        return RedirectToAction("States"); 
       } 
       else 
       { 
        return View("Edit", model); 
       } 
      } 
      else 
      { 
       return View("Edit", model); 
      } 

     } 


+0

你可以發佈兩種操作方法嗎? – Mariusz 2010-11-29 19:49:25

+0

已發佈代碼 – Blackcoil 2010-11-29 20:04:21

+0

在您最後的代碼示例中,「public ActionResult Edit(AccountStatusEditViewModel model)」出了什麼問題?該模型是有效的,並且在重定向到狀態視圖時正確更新。當你期望模型包含無效值時,它是否也這樣做? – Michel 2010-11-30 07:48:50

回答

0

由於2.0版本的MVC我沒有再使用formcollection。

我只使用操作的參數的視圖模型時,我有一個帖子,是這樣的:

[HttpPost] 
public ActionResult Activatecard(ActivateCardViewModel model) 
{ 

當「它」無法創建我的模型(進入布拉布拉的日期時間字段,或者當驗證不符合(我使用System.ComponentModel.DataAnnotations命名空間的驗證屬性)我得到的ModelState.IsValid等於false

我創建了一個空白的asp.net mvc2應用程序,這是用於登錄的標準模板行動。

0

請消除FromCollection參數。您可以爲視圖模型使用默認的ModelBinder。 Asp.net MVC嘗試將表單中的值映射到您的模型中。

您可以請發佈操作方法,這也會將您引導至編輯表單嗎?

相關問題