validation for Partialview

Asked

Viewed 296 times

1

I have a page that calls a partialview formed by a div with text field inside it, through a button. Each time the button is pressed a line is added. The problem is that I cannot validate the empty fields of this partialview.

I use the @Html.ValidationMessageFor

My View

@model CondicaoPagamentoDiasViewModel

@using (Html.BeginCollectionItem("CondicaoPagamentoDiass"))
{
    <div class="form-group">
        @Html.HiddenFor(model => model.CondicaoPagamentoDiasId)
        <label class="col-md-1 control-label"></label>
        <div class="col-md-10">
            <div class="col-md-1">
                @Html.EditorFor(model => model.Dias, new { htmlAttributes = new { @class = "form-control prazo" } })
                @Html.ValidationMessageFor(model => model.Dias, "", new { @class = "text-danger" })
            </div>
            <div class="col-md-2">
                <img src="@Url.Content("~/Content/img/excluir.png")" onclick="$(this).parent().parent().parent().remove();" style="cursor:pointer;">
            </div>
        </div>
    </div>
}
@{ Html.RenderPartial("_ValidationMaskPartial"); }

Minha Partialview

<div style="margin: 0 0 0 102px;" id="condicoes-pagamento" name="condicoes-pagamento">
                                @if (Model != null)
                                {
                                    foreach (var condicoesPgto in Model.CondicaoPagamentoDiass)
                                    {
                                        @Html.Partial("_CondicoesPagamentoDia", condicoesPgto);
                                    }
                                }
                            </div>

I call her here. I just want to validate her fields like you have on the main page.

  • You want to validate the partial fields _Conditions of payment?

  • That’s right. I want to validate as in the main, in it I have an equal and it validates.

  • You are doing validation by Data Annotation? If yes, can your entity with the annotations.

  • No, I use @Html.Validationmessagefor, this put in Viewmodel.

  • Yes, but you’re decorating your ViewModel with some note? [Required], [StringLength()], etc. Or using EF Fluent API. Validation by jQuery? What are you using for this?

  • I’m using [Required] myself.

Show 1 more comment

2 answers

0

I don’t know how you’re doing Viewmodel, but it is necessary to note [Required] in properties that cannot be empty for correct validation:

[Required]
public int Dias { get; set; }

Optionally, you can have Javascript validate the form for you before the POST, by inserting the following in View:

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

It is important to force validation on Controller also. This can be done as follows:

if (ModelState.IsValid) { ... }
  • Gypsy in my Viewmodel has the [Required], but even so it is not validating the Partial.

  • @jp_almeida I updated the answer.

  • Gypsy my controller already owns that if.

  • So I don’t know what it is.

  • Gypsy in If(modelState.isvalid) it does not pass when it is empty, can I take advantage of it and do the validation? ?

  • Yes, you can not validate when it is empty.

  • But doing the normal validation is not like in this Lse after the if, as it happens in the main page? I speak the message.

Show 2 more comments

0


I researched and found the following solution put in Partialview, on the part of scripts, worked right:

<script type="text/javascript">
        $("form").removeData("validator").removeData("unobtrusiveValidation");
        $.validator.unobtrusive.parse("form");
</script>
  • I didn’t understand. Why the unobtrusive validation would be getting in the way?

  • I did not understand Gypsy, I sought this solution and it worked for what I wanted to do. unobstrusiva is not disturbing.

Browser other questions tagged

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