Mr Lahey
Mr Lahey

Reputation: 676

Html.ValidationMessageFor not showing

I have a MVC2 Project using ADO.NET Entity Data Model that I used to generate my view automatically since it has tons of fields. It gave me code like this.

<% using (Html.BeginForm()) {%>
<%= Html.ValidationSummary(true) %>

<div class="editor-label">
    <%= Html.LabelFor(model => model.Position) %>
</div>
<div class="editor-field">
    <%= Html.TextBoxFor(model => model.Position) %>
    <%= Html.ValidationMessageFor(model => model.Position) %>
</div>

But with a ton more fields.

Here is my Controller code:

MyProjectEntities_Career_Applications dbca = new MyProjectEntities_Career_Applications();

[HttpPost]
    public ActionResult Apply(Career_Applications application)
    {
        if (ModelState.IsValid)
        {
            //save form
            dbca.AddToCareer_Applications(application);
            dbca.SaveChanges();
        }
        else
        {
            ModelState.AddModelError("", "Application could not be submitted.  Please correct the errors below.");
        }

        //redirect to thank you page
        return View("Apply", application);
    }

When the ModelState.IsValid fails I see the <%= Html.ValidationSummary(true) %> but none of the Html.ValidationMessageFor's ever show up.

What am I missing?

Upvotes: 0

Views: 2900

Answers (2)

Mr Lahey
Mr Lahey

Reputation: 676

Turns out the problem was with the CSS. Stupid mistake, ugh!

Thanks for the help regardless.

Upvotes: 1

RAS
RAS

Reputation: 3385

I think you're missing a Jquery reference or error handling library that you're using.

This also happens often when you include validation script multiple times. (In shared view, and in your view)

Upvotes: 0

Related Questions