2011-08-17 115 views
2

對JSON編碼數據返回true。我有一個表單,唯一需要的項目是客戶名稱。所以在我的模型中,我有:ASP.NET MVC 3 ModelState.IsValid始終使用Ext.Direct

[DisplayName("Customer name*:")] 
[Required] 
public string CustomerName 
{ get; set; } 

此前,我在做一個HTML文章,一切正常,包括驗證。

現在,我使用Ext.direct.mvc(http://code.google.com/p/ext-direct-mvc/)(這是一個重要的事實)將表單「ajaxified」,並以Json格式發佈數據,並且數據已成功發佈。

enter image description here

當我把一個斷點在我的代碼(目前修改調試目的):

[DirectInclude] 
    [HttpPost] 
    public ActionResult SaveOrUpdateOrderLines(CustomerOrderModel customerOrderModel) 
    { 

     if (!ModelState.IsValid) 
     { 
      return ModelState.JsonValidation(); 
     } 

     return null; 

我看到CustomerOrderModel.CustomerOrderHeader.CustomerName = 「」

enter image description here

但是ModelState.IsValid是true。

enter image description here

現在對於一些我試過的東西:

  1. 我已插入下面的代碼,只需檢查ModelState.isValid之前,爲了確保客戶名稱= NULL

    customerOrderModel.CustomerOrderHeader.CustomerName = null;

  2. 我一直在使用TryUpdateModel(customerOrderModel)嘗試,但我得到了以下錯誤消息:

    TryUpdateModel扔類型的異常「system.missingMethodException而」

  3. 我已經試過修改JSON數據等等「根」「CustomerOrderHeader」被重命名爲「customerOrderModel」以匹配參數。

這些都不起作用。那麼,我可能做錯了,驗證不再工作了?我可以採取哪些步驟來調試該問題?

編輯counsellorBen

enter image description here

EDIT 2 counsellorben

enter image description here

+0

好的我找到了這裏詳細的答案http://stackoverflow.com/questions/4465432/asp-net-mvc-2-controllers-tryvalidate-doesnt-validate-the-list-items-within/ 4489226#comment10011080_4489226。 – DavidS 2011-11-17 14:15:48

回答

0

的問題是試圖綁定一個JSON響應,在你的控制器動作變量的名稱時,必須匹配在客戶端傳遞的變量的名稱。您的模型是有效的,因爲CustomerOrderHeader爲null。

在您的客戶端腳本中,應該將整個模型包裝在名爲「customerOrderModel」的元素中,以便名稱與您的操作中的變量名稱相匹配。

+0

我在OP中添加了一個包含圖像的編輯部分。你是這個意思嗎?因爲如果是這樣,那仍然不起作用。 – DavidS 2011-08-17 13:03:26