2
I have a controller
where the information of the ModelState
, store errors in a list.
I would like to take this list and return to my view. But I’m a beginner and I’m not getting to understand how I can do this, because I could use the ValidationSummary
to list? or use a partial
for the return within the view
?
My controller:
public async Task<ActionResult> Create(ClienteViewModel viewmodel)
{
if (ModelState.IsValid)
{
db.Set<Pessoa>().Add(viewmodel.Pessoa);
if (viewmodel.Cliente.TipoPessoa.Equals(Models.Enum.TipoPessoa.Juridica))
{
// db.Set<PessoaJuridica>().Add(viewmodel.PessoaJuridica);
}
else
{
db.Set<PessoaFisica>().Add(viewmodel.PessoaFisica);
}
db.Cliente.Add(viewmodel.Cliente);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
else
{ /*Lista que quero retornar para minha view*/
var ListaErros = new List<string>();
foreach (var values in ModelState.Values)
{
foreach (var erros in values.Errors)
{
ListaErros.Add(erros.ErrorMessage);
}
}
}
return View(viewmodel);
}
View
:
@model Sistema.ViewModels.ClienteViewModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Cliente</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Cliente.TipoPessoa, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EnumDropDownListFor(model => model.Cliente.TipoPessoa, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Cliente.TipoPessoa, "", new { @class = "text-danger" })
</div>
</div>