MVC 3 client validation works intermittently

update: if you set at least one breakpoint in javascript, the check will start working, but without it it does not work

update: adding a jquery tag, as this may be due to the validation plugin

I have MVC 3 System.Web.Mvcversion, product version: 3.0.20105.0changed to 5th of Jan 2011- I think the latter.

Ive noticed that client validation is not working as expected in the application we are creating, so Ive done a quick test.

Ive created the base MVC 3 application using an Internet application template.

Added test controller:

using System.Web.Mvc;
using MvcApplication3.Models;

namespace MvcApplication3.Controllers
{
    public class TestController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create()
        {
            Sample model = new Sample();

            return View(model);
        }

        [HttpPost]
        public ActionResult Create(Sample model)
        {
            if(!ModelState.IsValid)
            {
                return View();
            }

            return RedirectToAction("Display");
        }

        public ActionResult Display()
        {
            Sample model = new Sample();
            model.Age = 10;
            model.CardNumber = "1324234";
            model.Email = "somedata@test.com";
            model.Title = "hahah";

            return View(model);
        }
    }
}

Model:

using System.ComponentModel.DataAnnotations;

namespace MvcApplication3.Models
{
    public class Sample
    {
        [Required]
        public string Title { get; set; }

        [Required]
        public string Email { get; set; }

        [Required]
        [Range(4, 120, ErrorMessage = "Oi! Common!")]
        public short Age { get; set; }

        [Required]
        public string CardNumber { get; set; }
    }
}

And 3 views:

Create:

@model MvcApplication3.Models.Sample

@{
    ViewBag.Title = "Create";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Create</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@*@{ Html.EnableClientValidation(); }*@

@using (Html.BeginForm()) {
    @Html.ValidationSummary(false)
    <fieldset>
        <legend>Sample</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Title)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Title)
            @Html.ValidationMessageFor(model => model.Title)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Email)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Age)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Age)
            @Html.ValidationMessageFor(model => model.Age)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.CardNumber)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.CardNumber)
            @Html.ValidationMessageFor(model => model.CardNumber)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>

    @*<fieldset>

        @Html.EditorForModel()

        <p>
            <input type="submit" value="Create" />
        </p>

    </fieldset>    *@           
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

Screen:

@model MvcApplication3.Models.Sample

@{
    ViewBag.Title = "Display";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Display</h2>

<fieldset>
    <legend>Sample</legend>

    <div class="display-label">Title</div>
    <div class="display-field">@Model.Title</div>

    <div class="display-label">Email</div>
    <div class="display-field">@Model.Email</div>

    <div class="display-label">Age</div>
    <div class="display-field">@Model.Age</div>

    <div class="display-label">CardNumber</div>
    <div class="display-field">@Model.CardNumber</div>
</fieldset>
<p>
    @Html.ActionLink("Back to List", "Index")
</p>

Index

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create", "Create")
</p>

<p>
    @Html.ActionLink("Display", "Display")
</p>

- Controller, AddView , .

/Test/Create client validation, Title Age, "" ( ).

( ) Title Email , CardNumber Title CardNumber, Email . "".

Ive Html.EditorForModel, BeginForm:

@{ Html.EnableClientValidation(); }

Im Dropbox - , , dev env :/Ive IE 8 Chrome 10 .

, - :

<appSettings>
  <add key="ClientValidationEnabled" value="true"/> 
  <add key="UnobtrusiveJavaScriptEnabled" value="true"/> 
</appSettings>

,

, , , ?

- /?

update: javascript, ,

update: jquery,

+5
2

- . ( "" ) .

0

script <head>. , .

-1

All Articles