Form does not validate using Partial View

Asked

Viewed 139 times

0

I’m using Partial View for registration of Pessoa, that is, in my View have two RadioButton: Pessoa Fisica and Pessoa Juridica, when selecting the Radio Button I’ll tell you what ajax:

    $(function () {
    $(".radioPessoa").change(function (event) {
        var opcao = $(this).val();
        $.ajax({
            url: '@Url.Action("OpcaoPessoa", "Cadastro")',
            data: { opcao: opcao },
            type: 'GET',
            success: function (data) {
                $(".loadpartial").html(data);
            }
        });
    });
});

There, I have these two files .cshtml containing the necessary inputs, Everything is working so far, BUT, when I give Submit, it does not validate my inputs, which put the requirements in my Model

In my View Create I left the class to receive the inputs and the submit:

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<div class="loadpartial">
</div>

<div class="col-xs-12 col-md-12 semPadding marginCimaBaixo clearfix">
    <input type="submit" value="Salvar" class="btn btn-success" />
</div>

}

my contents of the archives .cshtml will press the div , segue meuModel: `

    [Required(ErrorMessage = "Campo CPF deve ser preenchido")]
    [RegularExpression(@"[0-9.-]{14}", ErrorMessage = "Por favor, preencha o CPF apenas com números.")]
    public string CPF { get; set; }

summarizing, it does not validate my inputs and goes to Controller

1 answer

0


I put this code in after I got the AJAX

 //allow the validation framework to re-prase the DOM
 jQuery.validator.unobtrusive.parse(); 

 //or to give the parser some context, supply it with a selector
 //jQuery validator will parse all child elements (deep) starting
 //from the selector element supplied
 jQuery.validator.unobtrusive.parse("#formId");

Browser other questions tagged

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