ValidationSummary () not showing when using RenderPartial in view

When i use

 @{Html.RenderPartial("Login");} 

      

inside my main view, @Html.ValidationSummary()

doesn't work, but when I copy the code from "Input" in the main view, it works.

Why is this and how to display validation messages from partial view?

Here is a partial view of the "Login":

@model NyNo.Models.LoginModel

@using (Html.BeginForm())
{
    <fieldset>
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)

        @Html.TextBoxFor(m => m.UserName, new { @placeholder = "Username" })
        @Html.ValidationMessageFor(m => m.UserName)

        @Html.PasswordFor(m => m.Password, new { @placeholder = "Password" })
        @Html.ValidationMessageFor(m => m.Password)

        @Html.CheckBoxFor(m => m.RememberMe)
        @Html.LabelFor(m => m.RememberMe, new { @class = "checkbox" })

        <input type="submit" class="button" value="Log in" />
    </fieldset>
}

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

      

Hope you can understand, thanks!

+3


source to share


2 answers


Unfortunately, this won't work. A Partial is just a string.



While RenderPartial actually "writes" the partial markup rather than returning the string back to the view generator, it does not reset your view to the new model. If you want the validation summary to work, it must be model-bound in your main view.

+4


source


Your problem might be related to this (you may not be showing the ViewData passed to RenderPartial()

): Pass additional ViewData to ASP.NET MVC 4 Partial View while propagating ModelState errors



I had a similar problem and solved it as follows: ValidationSummary inside a partial view showing no errors

0


source







All Articles