2010-10-29 124 views
2

我在使用DropDownList驗證的ASP.NET MVC中遇到問題。 我有兩個操作「創建」。它們的定義如下:DropDownList驗證 - 問題

public ActionResult Create() 
    { 
     var categoriasDownloads = from catDown in modelo.tbCategoriasDownloads 
            orderby catDown.TituloCategoriaDownload ascending 
            select catDown; 

     ViewData["CategoriasDownloads"] = new SelectList(categoriasDownloads, "IDCategoriaDownloads", "TituloCategoriaDownload"); 

     var formatosArquivos = from formatosDown in modelo.tbFormatosArquivos 
           orderby formatosDown.NomeFormatoSigla 
           select formatosDown; 

     ViewData["FormatosArquivos"] = new SelectList(formatosArquivos, "IDFormatoArquivo", "NomeFormatoSigla"); 

     return View(); 
    } 

,第二個操作創建爲:

[HttpPost] 
    public ActionResult Create(tbDownloads _novoDownload) 
    { 
     TryUpdateModel(modelo); 
     TryUpdateModel(modelo.tbDownloads); 

     if (ModelState.IsValid) 
     { 
      modelo.AddTotbDownloads(_novoDownload); 
      modelo.SaveChanges(); 

      return RedirectToAction("Sucesso", "Mensagens"); 
     } 

     return View(_novoDownload); 
    } 

的問題是:當試圖驗證,不會發生驗證。我正在使用數據註釋進行驗證,但我還沒有成功。

我該怎麼辦?

謝謝

+1

發表您的數據註釋代碼,請 – Arief 2010-10-29 14:09:27

回答

0

驗證發生,但您正在驗證錯誤的對象。

WRONG:

TryUpdateModel(modelo); 
TryUpdateModel(modelo.tbDownloads); 

正確:

TryUpdateModel(_novoDownload); 
+0

的問題仍然存在.. 。 – 2010-11-01 12:23:20

+0

請發佈tbDownloads類(您指定驗證的類)和th e view Create.aspx – 2010-11-02 16:54:57

0

$ .validator.addMethod( 「選擇無」,

 function (value, element) { 
      return this.optional(element) || element.selectedIndex != 0; 
     }, 
     "Please select an option." 
    ); 


    $(function() { 
     $("#form1").validate({ 
      rules: { 
       ProductCategoryList: { 
        selectNone: true 
       } 

      }, 
      messages: { 
       ProductCategoryList: { 
        selectNone: "This field is required" 
       } 
      } 
     }); 
    }); 
+0

在你的頁面上使用這個jquery驗證 – yogee 2011-02-14 11:32:56