2010-10-29 162 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