I've been working on writing my first MVC3 application for a little while and I have encountered a bug that I am not able to resolve. Client side validation does not appear to be working. I keep getting: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
I have done a lot of research on this problem and have found quite a few people that have had similar issues whose solutions do not work for me.
My Web Config File Contains:
<appSettings>
<add key="webpages:Version" value="1.0.0.0" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="RouteDebugger:Enabled" value="false" />
</appSettings>
I have setup data annotations on all of my Model classes:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace Mike_Cottingham_Industries.Models
{
public class Part
{
public Int32 PartID { get; set; }
[Display(Name="Manufacturer Part Number")]
[Required(ErrorMessage="Manufacturer Part Number is required")]
[MaxLength(50, ErrorMessage="Manufacturer Part Number cannot exceed 50 characters")]
public String ManufacturerPartNumber { get; set; }
[Display(Name="Supplier Part Number")]
[Required(ErrorMessage="Supplier Part Number is required")]
[MaxLength(50, ErrorMessage="Supplier Part Number cannot exceed 50 characters")]
public String SupplierPartNumber { get; set; }
[Required(ErrorMessage="Description is required")]
[MaxLength(250, ErrorMessage="Description cannot exceed 250 characters")]
public String Description { get; set; }
public virtual ICollection<Product> Products { get; set; }
public virtual ICollection<PartOrder> PartOrders { get; set; }
}
}
I have the data validation scripts loaded in my views:
<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>
The only thing that I can think of that could be messing it up is the way that I have tried to make generic views.
I have created a generic 'Create' view:
<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((string)@ViewBag.Action, (string)@ViewBag.ControllerName, FormMethod.Post, new { @class="niceform"})) {
<fieldset class="edit_item">
@Html.Partial("_Edit");
</fieldset>
}
<div>
<a href="@Url.Action("Index", (string)@ViewBag.ControllerName)" class="bt_green"><span class="bt_green_lft"></span><strong>Back to List</strong><span class="bt_green_r"></span></a>
<a href="#" class="bt_blue" onclick="$('.niceform').submit(); return false;"><span class="bt_blue_lft"></span><strong>Save</strong><span class="bt_blue_r"></span></a>
</div>
And a partial that gets loaded by the generic view:
@model Mike_Cottingham_Industries.Models.Part
@{
ViewBag.Controller = "Part";
ViewBag.Title = "Part";
}
@if (ViewBag.IsEdit == true)
{
@Html.HiddenFor(model => model.PartID)
}
@Html.ValidationSummary(true)
<dl>
<dt>@Html.LabelFor(model => model.ManufacturerPartNumber, "Manufacturer Part Number")</dt>
<dd>@Html.EditorFor(model => model.ManufacturerPartNumber)
@Html.ValidationMessageFor(model => model.ManufacturerPartNumber)</dd>
</dl>
<dl>
<dt>@Html.LabelFor(model => model.SupplierPartNumber)</dt>
<dd>@Html.EditorFor(model => model.SupplierPartNumber)
@Html.ValidationMessageFor(model => model.SupplierPartNumber)</dd>
</dl>
<dl>
<dt>@Html.LabelFor(model => model.Description)</dt>
<dd>@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)</dd>
</dl>
Does anyone have any thoughts as to what I'm doing wrong? Views that do not use my 'generic' appear to be working fine.
Cheers!
Not really sure what the issues was... I copied all of my code into a new project and everything worked fine. Can't explain what was different.