0

PROBLEM SOLVED

Basically I'm used to starting my ASP.NET MVC4 projects with the Internet template. However this time I tried out with the Basic template. Now, I'm trying to manually add jQuery validation: I've installed the NuGet package jQuery.validation and have included it in my bundleconfig and _layout file.

Here's my bundleconfig:

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.unobtrusive*",
                        "~/Scripts/jquery.validate*"));

And here's how I include it in _layout:

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")

Here are the js files being loaded per request:

http://localhost:10815/Scripts/jquery-2.0.3.js
http://localhost:10815/Scripts/jquery.unobtrusive-ajax.js
http://localhost:10815/Scripts/jquery.validate.js
http://localhost:10815/Scripts/jquery.validate.unobtrusive.js

In my controller I check if ModelState.IsValid and if not I return the view with the model - I've debugged the method and know for sure that the modelstate has errors.

And finally in my view file I have included:

@Html.ValidationSummary()

Which produces the following HTML output:

<div class="validation-summary-valid" data-valmsg-summary="true"><ul><li style="display:none"></li>
</ul></div>

My issue is the fact that the validation summary doesn't change when I the controller returns the view - I don't know what's wrong though - what am I missing?

PROBLEM SOLVED - I used ajax which I've turned off now - works fine without ajax. I guess the validationsummary doesnt get reloaded thus no html is updated.

Jordan Axe
  • 3,823
  • 6
  • 20
  • 27
  • Are you posting data initially to this method as it's a HttpPost type, or is there a corresponding Get method you are initially rendering? – ianaldo21 Oct 24 '13 at 21:15
  • @ianaldo21 Im posting the data to the post method. Initially I'm loading the view through an empty get method that just returns the view. – Jordan Axe Oct 24 '13 at 21:17
  • Please don't put the solution inside your question. Instead, post the answer below as soon as the site allows. – Sparky Oct 25 '13 at 00:42

3 Answers3

0

For the validation to work, you need to have a model property with the name of Test error

EDIT:

You need to place data annotations in your model.

  public class Contact
    {
        [Required]
        public string MobileNumber { get; set; }
    }
WannaCSharp
  • 1,898
  • 2
  • 13
  • 19
  • Nevermind the manually added error - I've removed it and I still get the same result even though I enter invalid input in the form. – Jordan Axe Oct 24 '13 at 21:26
  • I already have data annotations - I KNOW for a fact that it catches the errors - however it is not being displayed in the view. – Jordan Axe Oct 24 '13 at 21:32
  • Did you use `TextBoxFor` to render the fields of the form? – WannaCSharp Oct 24 '13 at 21:45
  • I've added TextBoxFor - it still doesn't show in validationsummary but it now displays an error in @Html.ValidationMessageFor I wonder why it doesn't get displayed in validationsummary though – Jordan Axe Oct 24 '13 at 22:07
  • Ahh apparently it doesn't work when the form is AJAX - I didn't know that - thanks for your assistance anyways – Jordan Axe Oct 24 '13 at 22:13
0

See this answer. If you define a key there must be a match in the model.

Community
  • 1
  • 1
developer10214
  • 1,128
  • 1
  • 11
  • 24
  • Nevermind the manually added error - I've removed it and I still get the same result even though I enter invalid input in the form. – Jordan Axe Oct 24 '13 at 21:26
0

I used ajax which I've turned off now - works fine without ajax. I guess the validationsummary doesnt get reloaded thus no html is updated.

Jordan Axe
  • 3,823
  • 6
  • 20
  • 27