Error In Modelstate Validation

Asked

Viewed 764 times

0

I am wanting to return the validation errors of my model. Only I get an error message when I try to register.

System.Linq.Enumerable+Whereenumerableiterator`1[System.Web.Mvc.Modelerrorcollection]

I’m putting the validation on 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 {
            string erros = ModelState.Select(x => x.Value.Errors).Where(y => y.Count() > 0).ToString();
            ModelState.AddModelError("", erros);

        }

       // ViewBag.PessoaId = new SelectList(db.Pessoa, "PessoaId", "Nome", cliente.PessoaId);
        return View(viewmodel);
    }

In the Viewmodel only stance the classes Pessoa, PessoaJuridica and Fisica.

EDIT

The error happens on this line where return to view.

string erros = ModelState.Select(x => x.Value.Errors).Where(y => y.Count() > 0).ToString();
            ModelState.AddModelError("", erros);
  • Do you have a complete error? It happens at runtime?

  • On which line is this error generated? No if (ModelState.IsValid) ?

  • I added the line, the error is stored inside the error variable.

3 answers

2

This is wrong, you can’t do it this way:

string erros = ModelState.Select(x => x.Value.Errors).Where(y => y.Count() > 0).ToString();

You are turning a collection into a string and when you do that the string that is returned is this

System.Linq.Enumerable+Whereenumerableiterator`1[System.Web.Mvc.Modelerrorcollection]

Solution

The correct thing is to turn the collection items into string, a possible solution would be:

whereas x.Value.Errors is a collection

string erros =string.Join(", ",ModelState.Select(x => x.Value.Errors).Where(y => y.Count() > 0).SelectAll(z => z.ToArray() ));

So you put all the items in the collection together and turn them into a string separated by comma.

  • Ceerto, but that way I get that mistake: System.Web.Mvc.ModelErrorCollection

  • x.Value.Errors this Errors is a collection?

  • I made an amendment considering that x.Value.Errors is a collection

0

I used to foreach and it worked that way:

var ListaErros = new List<string>();
                foreach (var values in ModelState.Values)
                {
                    foreach (var erros in values.Errors)
                    {
                        ListaErros.Add(erros.ErrorMessage);
                    }
                }

-2

Get the list of errors

var ListErro = ModelState.Where(a => a.Value.ValidationState == ModelValidationState.Invalid).Select(a => a.Key + " " + a.Value.Errors[0].ErrorMessage).ToList()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.