validation for PartialView

1

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

I use @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"); }

My 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 it here. I just want to validate her fields as you have on the main page.

    
asked by anonymous 23.09.2015 / 21:18

2 answers

0

I searched and found the following solution placed in the PartialView , part of scripts , worked fine:

<script type="text/javascript">
        $("form").removeData("validator").removeData("unobtrusiveValidation");
        $.validator.unobtrusive.parse("form");
</script>
    
25.09.2015 / 21:23
0

I do not know how your ViewModel is, but you need to note [Required] " in properties that can not be empty for the correct validation:

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

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

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

It's important to force validation on Controller as well. This can be done as follows:

if (ModelState.IsValid) { ... }
    
23.09.2015 / 21:37