2012-08-28 45 views
2

我有一個具有數字和字符串屬性的模型,每個屬性都有[必需的]驗證註釋。我也有相應的觀點,這是該模型的輸入形式。出於某種原因,一旦加載視圖,字符串屬性所需的驗證消息立即顯示,而不是數字屬性所需的驗證消息,只有當用戶嘗試提交表單(如預期的那樣)時才顯示。有沒有人有關於字符串屬性的奇怪驗證行爲的線索?MVC3在表單提交之前顯示的[必需的]驗證消息

更新

我縮小了問題的「複雜」的方式我顯示從控制器操作的視圖。就我而言,我有一個控制器操作Create,負責創建具有許多屬性的實體。由於實體具有許多屬性,因此我通過服務器端嚮導將此過程分解爲幾個步驟。以下是我的控制器操作的簡化版本:

public ActionResult Create() 
    { 
     Model = new CreateEditListingViewModel(); 
     return View("StepOne"); 
    } 

    [HttpPost] 
    public ActionResult Create(string buttonValue, StepOneViewModel stepOneModel, StepTwoViewModel stepTwoModel, StepThreeViewModel stepThreeModel) 
    { 
     ActionResult nextView = null; 
     CreateListingSteps step = (CreateListingSteps)Enum.Parse(typeof(CreateListingSteps), buttonValue); 
     // Save the value of the step that has been submitted and redirect user to next step 
     switch (step) 
     { 
      case CreateListingSteps.StepOne: 
       Model.StepOne = stepOneModel; 
       nextView = View("StepTwo"); 
       break; 
      case CreateListingSteps.StepTwo: 
       Model.StepTwo = stepTwoModel; 
       nextView = View("StepThree"); 
       break; 
      case CreateListingSteps.StepThree: 
       Model.StepThree = stepThreeModel; 
       nextView = View("Confirm"); 
       break; 
     } 
     return nextView; 
    } 

顯然發生了什麼事是,一旦用戶點擊下一步按鈕,在第一時間(在所有的步驟圖用),驗證觸發所有後續形式,因此即使用戶尚未提交表單,也不合理地將字符串字段顯示爲無效。

Blank form that hasn't been submitted showing required message for description field. Price appears regularly because its data type is not string

誰能想到解決類似的問題呢?

+0

你是否將字符串字段初始化爲非空值? –

+1

你有任何JavaScript綁定到各自的'輸入'? –

+0

@AndreCalil號我試着改變模型中的屬性名稱並查看,以防萬一有一些我不知道的JavaScript綁定,但仍然無效。 – user1447435

回答

1

嘗試使用String.Empty初始化您的字符串。

+0

試過了,沒有工作。 – user1447435

4

我解決了它。我需要的只是在返回新視圖之前調用ModelState.Clear()

相關問題