Modelstate.Isvalid in modal registration

Asked

Viewed 266 times

0

I am using a registration form within a modal, however when the condition of the ModelState.IsValid is not satisfied with view is returned. Only instead of returning the same view before the action of submission of the form, it returns totally disfigured, containing only the view registration. Follows the code of the view criminal record:

[HttpPost]
public ActionResult Cadastrar(Pessoa pessoa)
{
    if (ModelState.IsValid)
    {
        //código caso seja válido
    }
    return View(pessoa);
}

How to proceed to display errors in view for the user?

1 answer

1

Colleague, as you are calling this View, usually when we create modal we do by ajax and change only part of the code and the View Return is usually done as

return PartialView(Pessoa)

I advise you to handle all errors in a Javascript function by intercepting the form’s Submit.

Your Form

    <form id="FormCadastro" action="~/Alunos/Cadastro" method="post" class="form-horizontal form-enter-cancel" onsubmit="FormSubmit(this); return false;">

example of Submit function

 function FormSubmit(frm) {

    var erros = 0;
    var mErro = '';

    if (frm.idMovimentacao.value == 0) {
        erros++;
        erroValidacao("idMovimentacao", "Selecione o tipo de Movimentação");
        frm.idMovimentacao.focus();
    }

    if (frm.idSituacao.value == 0) {
        erros++;
        erroValidacao("idSituacao", "Selecione a situação");
        frm.idSituacao.focus();
    }

    if (erros > 0) {
        if (mErro != '') {
            MensagemTexto(mErro);
        }
        return false;
    }

    if ($("#FormCadastro").valid() == false) {
            erros++;
    }

    if (erros > 0) {
            return false;
    }
    else {
          frm.submit();
    }

}; 

Express Code

Browser other questions tagged

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