6

I have following ViewModel:

public class Bulletin1ViewModel
    {
        [Required]
        public String NumberDelegations { get; set; }

        [Required]
        public String TravelPlans { get; set; }
    }

Which I want to use in my View:

     @using ErasProject.Models
        @model ErasProject.Models.Bulletin1ViewModel
<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
<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>

        @using (Html.BeginForm())
        { 
            @Html.ValidationSummary(true)

            <fieldset>

            <p>
            @Html.EditorFor(model => model.NumberDelegations)
            @Html.ValidationMessageFor(model => model.NumberDelegations)
            </p>

            <p>
            @Html.EditorFor(model => model.TravelPlans)
            @Html.ValidationMessageFor(model => model.TravelPlans)
            </p>

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

            </fieldset>

    }

But my validation isn't triggered. Neither client-side nor server-side. Anyone could have an idea why? Thanks.

2 Answers 2

5

you need to add both jquery and jQuery Validation plugin (and optionally, the unobtrusive library)

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"           type="text/javascript"></script>
<script src="http://ajax.microsoft.com/ajax/jQuery.Validate/1.7/jQuery.Validate.min.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>

so, your view will be as:

@using ErasProject.Models
@model ErasProject.Models.Bulletin1ViewModel

<script src="jquery.min.js"></script>
<script src="jQuery.Validate.min.js"></script>
<script src="jquery.validate.unobtrusive.min.js"></script>

@using (Html.BeginForm())
{ 
    @Html.ValidationSummary(true)

    <fieldset>

    <p>
    @Html.EditorFor(model => model.NumberDelegations)
    @Html.ValidationMessageFor(model => model.NumberDelegations)
    </p>

    <p>
    @Html.EditorFor(model => model.TravelPlans)
    @Html.ValidationMessageFor(model => model.TravelPlans)
    </p>

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

    </fieldset>

}

Just added your object and created a Add Action like:

public ActionResult Add()
{
    return View();
}

and created a View using the Create template and the Bulletin1ViewModel class that look like this:

@model WebApp_MVC3.Models.Bulletin1ViewModel

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

<h2>Add</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>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Bulletin1ViewModel</legend>

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

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

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

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

without doing anything more, the result is:

original file

enter image description here

I would re-check the javascript libraries...

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

3 Comments

They are already added. It's an MVC3 application with RazorView engine. And I've checked they are indeed present.
they load correctly when you see the source of your page? did you build?
I reloaded the javascript libraries and it works now. thank you!
1

Don't use

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

but just drag and drop the following .js files from your Scripts folder in your project:

jquery-1.4.4.min.js
jquery.validate.min.js
jquery.validate.unobtrusive.min.js

That will make sure your references to the js files are correct.

1 Comment

do an Update using Web Platform Installer or using the power shell... jQuery is already in version 1.6.1 :)

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.