4

When I use

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

inside my main view, the @Html.ValidationSummary() doesn't work, but when I copy the code from "Login" inside main view, it works.

Why is that, and how do I display validation messages from the partial view?

Here is partial view "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 understand, thanks!

2 Answers 2

4

Unfortunately, this cannot work. A Partial is just a string.

While RenderPartial actually 'writes' the partial markup rather than sending a string back to the View Generator, it does not rebind your View to a new model. If you want Validation Summary to work it must be bound to a model in your main View.

Sign up to request clarification or add additional context in comments.

3 Comments

Allright, I'll take that as an answer, thanks! My problem now is that I want my login code above and a register form on the same page, but I can't have 2 model references at the top of the View, like this: model NyNo.Models.LoginMode model NyNo.Models.RegisterModel Any simple solution on this problem?
@Lars that's a good question. You should create a new post explaining in detail.
@Lars You can solve the problem of multiple models in a view by creating a complex model that contains the submodels. See this answer stackoverflow.com/a/9998040/649497 for a good example of how to do this.
0

Your problem could be related to this (maybe you aren't showing the ViewData passed in the RenderPartial()): Pass Additional ViewData to an ASP.NET MVC 4 Partial View While Propagating ModelState Errors

I was having a similar issue and I solved it this way: ValidationSummary inside a partial view not showing errors

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.