Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValidationSummary inside a partial view not showing errors

I have a partial view like this (simplified):

@model Portal.Models.LoginModel

<div class="login-container k-block">

    <section id="login-form" class="">
        @using (Html.BeginForm(actionName, controllerName, new { ReturnUrl = ViewBag.ReturnUrl }))
        {
            @Html.AntiForgeryToken()
            @Html.ValidationSummary(true)

            <fieldset id="login-form-list-items">
                <ol>
                <li>
                    @Html.LabelFor(m => m.CardNumber)
                    @Html.TextBoxFor(m => m.CardNumber, new { @class="k-textbox"})
                    <div class="k-error-colored">
                        @Html.ValidationMessageFor(m => m.CardNumber)
                    </div>
                </li>
                <li>
                    @Html.LabelFor(m => m.Pin)
                    @Html.PasswordFor(m => m.Pin, new { @class="k-textbox"})
                    <div class="k-error-colored">
                        @Html.ValidationMessageFor(m => m.Pin)
                    </div>
                </li>
                <input id="login-input-submit" class="k-button" type="submit" value="Enter" />
            </fieldset>

</div>

And in my login view I call this partial view like:

@model Portal.Models.LoginModel

@Html.Partial("_LoginFormNoRegistration", Model, new ViewDataDictionary { { "actionName", "Login" }, { "controllerName", "Account" } })

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

The problem is that when the login method in the controller adds an error like:

    public ActionResult Login(LoginModel model, string returnUrl)
    {
        //...

        // If we got this far, something failed, redisplay form
        ModelState.AddModelError("", "The user name or password provided is incorrect.");
        return View(model);
    }

The message is not show in the validation summary... I don't understand why... What could be the problem? Some javascript library missing?

Update

I also found that the form generated as the novalidate attribute set:

<form action="/" method="post" novalidate="novalidate">
//...
</form>

I don't know why.

like image 741
amp Avatar asked Jul 25 '13 14:07

amp


People also ask

Can I use ViewBag in partial view?

We can also bind partial view directly from a model. We can also bind partial view with ViewBag.

How to show Validation message in c#?

ValidationSummary() helper is called immediately above the HTML form. This helper is used to display a list of validation error messages. The Html. ValidationSummary() helper renders the errors in a bulleted list.

What is html ValidationSummary true?

The ValidationSummary() extension method displays a summary of all validation errors on a web page as an unordered list element. It can also be used to display custom error messages.


1 Answers

I found the problem.

I was passing a new ViewData in the RenderPartial which was overriding the ViewData of the parent view, so the model state was lost, as explained here: Pass Additional ViewData to an ASP.NET MVC 4 Partial View While Propagating ModelState Errors.

Changing the main view to:

@model Portal.Models.LoginModel

@{
    ViewData.Add("actionName", "Login");
    ViewData.Add("controllerName", "Account");
    Html.RenderPartial("_LoginFormNoRegistration", Model, ViewData);
}

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

Did the trick!

Also, if you want to show a general error message for the model in the validationsummary, be sure to add the error with an empty string as key:

ModelState.AddModelError("error", "The user name or password provided is incorrect."); - doesn't work

ModelState.AddModelError("", "The user name or password provided is incorrect."); - works

like image 193
amp Avatar answered Oct 04 '22 20:10

amp