James South
James South

Reputation: 10645

viewmodels and unobtrusive validation mvc3

I'm unable to get unobtrusive validation to work using custom viewModels representing abstractions of my EF generated classes.

As try as I might, the validation is not firing on form submission for the property Name in my City entity. I believe it is has something to do with the different models in the views but I simply don't know enough how it all works.

Please note. I have all the latest validation scripts and when observing the page using firebug and firequery I can see that the script is adding and removing the class valid from the input but that the input is not part of the validation collection.

Many thanks in advance.

My viewModel:

/// <summary>
/// Represents abstraction of the City View that also serves in
/// data binding between the City View and the City Model.
/// </summary>
public class CityViewModel
{
    /// <summary>
    /// Gets or sets the city.
    /// </summary>
    /// <value></value>
    public City City { get; set; }

    /// <summary>
    /// Gets or sets the collection of states.
    /// </summary>
    /// <value></value>
    public ICollection<State> States { get; set; }
}

My CreateCity view:

@model OzFarmGuide.ViewModels.CityViewModel
@{
    ViewBag.Title = "Create a new city";
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
<h2>
    Create a new city</h2>

@using (Html.BeginForm())
{
    @Html.EditorFor(model => model.City, new { States = Model.States })
    <div class="entity-actions">
        <input type="submit" value="Create" />
        |
        @Html.ActionLink("Back to List", "Cities")
    </div>
}

My Editor template: (_ValidationPartial just contains the script references)

@model OzFarmGuide.Models.City
@Html.Partial("_ValidationPartial")
@Html.ValidationSummary(true)
<div class="editor-label">
    @Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
    @Html.TextBoxFor(model => model.Name)
    @Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
    @Html.LabelFor(model => model.State)
</div>
<div class="editor-field">
    @Html.DropDownList("StateId",
    new SelectList(ViewBag.States as System.Collections.IEnumerable,
    "StateId", "Name",
    Model.StateId))
</div>
@Html.HiddenFor(model => model.CityId)

As requested here are the scripts I have included:

<script src="@Url.Content("http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

Upvotes: 1

Views: 1720

Answers (3)

BentOnCoding
BentOnCoding

Reputation: 28228

Have you set the following ?

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

You can also turn them on or off with code:

HtmlHelper.ClientValidationEnabled = true;
HtmlHelper.UnobtrusiveJavaScriptEnabled = true;

Also, have you added any of the required data annotations to your properties ?

Brad Wilson On Unobtrusive Validation

Upvotes: 0

amit_g
amit_g

Reputation: 31270

You need to add [Required] data annotations on all reference types and string types. As you mentioned that your classes are EF generated. For that use buddy class. Look at the answer in another similar question.

Upvotes: 3

hazimdikenli
hazimdikenli

Reputation: 6029

1- For string properties you should add [Required].

2- Can you check if you are having multiple input fields with the same name "Name", this could be the problem.

Upvotes: 0

Related Questions