In the scenario below, I have a View
that depends on some ViewBags
to fill options
on selects
html
. When performing POST
and in case something goes wrong in the register, the user is redirected to the same View
with the filled fields. In this method POST
, I am currently replicating the creation of ViewBags
. Is this the best approach? Is it correct to put the list of metadata (States, cities, neighborhoods, countries) in ViewModel
and load selects
by it?
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);
}
}