开发者

viewmodels and unobtrusive validation mvc3

开发者 https://www.devze.com 2023-02-28 08:51 出处:网络
I\'m unable to get unobtrusive validation to work using custom viewModels representing abstractions of my EF generated classes.

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 i开发者_运维技巧ncluded:

<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>


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消