3
In the scenario below, I own a View
that depends on some ViewBags
to fill in options
in selects
html
. When performing the POST
and if something goes wrong in the registration, the user is redirected to the same View
with fields filled in. In this method POST
, I am currently replicating the creation of ViewBags
. Is this the best approach? It is correct to put the meta data list (states, cities, neighborhoods, countries) in the ViewModel
and load the selects
for his sake?
public ActionResult Cadastrar()
{
ViewBag.Estados =[...]; //List de selectListItem
ViewBag.Cidades = [...];
ViewBag.Bairros = [...];
ViewBag.Paises = [...];
return View();
}
[HttpPost
public ActionResult Cadastrar(CadastrarUsuarioViewModel model)
{
ViewBag.Estados =[...]; //List de selectListItem
ViewBag.Cidades = [...];
ViewBag.Bairros = [...];
ViewBag.Paises = [...];
try
{
//DoSomething
ExibirMensagemSucesso();
return RedirectToAction("Index");
}catch (Exception ex)
{
//Do Something
return View(model);
}
}