开发者

DropDownList Validation - Problem

I'm having a problem in ASP.NET MVC with DropDownList validation开发者_开发百科. I have two actions "Create". They are defined as follow:

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();
    }

and the second action Create is:

[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);
    }

The problem is: When a try to validate, the validation does not occur. I'm using Data Annotations to validate, but i have not been sucessful.

What should i do?

Thanks


The validation occur but you are validating the wrong object.

WRONG:

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

CORRECT:

TryUpdateModel(_novoDownload);


$.validator.addMethod("selectNone",

        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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜