views:

58

answers:

2

Hello guys...

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

A: 

The validation occur but you are validating the wrong object.

WRONG:

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

CORRECT:

TryUpdateModel(_novoDownload);
Bugeo