Reputation: 141
I've been racking my brain but can't see what's wrong with this. Validation is working fine throughout the site, but it doesn't fire for this input. I'm using .NET 4, MVC 4, EF 5, jQuery 1.8. Any ideas?
Schedule.cs
{
[MetadataType(typeof(ScheduleMetaData))]
public partial class Schedule
{
}
public class ScheduleMetaData
{
[RegularExpression(@"^#(([0-9a-fA-F]{2}){3}|([0-9a-fA-F]){3})$", ErrorMessage = "Invalid hex color")]
[Required]
public string Color { get; set; }
}
}
Schedule.cs (built by EF)
{
using System;
using System.Collections.Generic;
public partial class Schedule
{
public Schedule()
{
this.OriginDependency = new HashSet<Dependency>();
this.EndpointDependency = new HashSet<Dependency>();
}
public int ScheduleId { get; set; }
public string ScheduleName { get; set; }
public string Color { get; set; }
public virtual ICollection<Dependency> OriginDependency { get; set; }
public virtual ICollection<Dependency> EndpointDependency { get; set; }
}
}
Edit.cshtml
<div class="editor-label">
@Html.LabelFor(model => model.Color)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Color)
@Html.ValidationMessageFor(model => model.Color)
</div>
BundleConfig.cs (snippet)
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.validate*",
"~/Scripts/jquery.unobtrusive*"));
Upvotes: 3
Views: 337
Reputation: 1482
Where is your script bundle included? If you include the bundle too late the validation won't work because it can't find the jquery.validate
library. Please check where your bundle include is done.
You can also use Firebug in Firefox (or Chrome's Developer Tools), go to the Net tab and check that jquery.validate is correctly loaded. While you're in there, check for any other javascript errors (on the console tab).
Upvotes: 0
Reputation: 4353
Probably you have different namespaces due to the folder they are located into, or maybe there are different assemblies that hold the two classes.
Also due to lack of details from Edit.Cshtml there might be a missing include:
Scripts.Render("~/bundles/jqueryval")
in that View/PartialView(Check browser console for errors).
Make sure that both classes have the same namespace:
namespace ExampleNamespace
{
[MetadataType(typeof(ScheduleMetaData))]
public partial class Schedule
{
}
public class ScheduleMetaData
{
[RegularExpression(@"^#(([0-9a-fA-F]{2}){3}|([0-9a-fA-F]){3})$", ErrorMessage = "Invalid hex color")]
[Required]
public string Color { get; set; }
}
}
Where ExampleNamespace should be taken from here: EF generated class. namespace ExampleNamespace { using System; using System.Collections.Generic;
public partial class Schedule
{
public Schedule()
{
this.OriginDependency = new HashSet<Dependency>();
this.EndpointDependency = new HashSet<Dependency>();
}
public int ScheduleId { get; set; }
public string ScheduleName { get; set; }
public string Color { get; set; }
public virtual ICollection<Dependency> OriginDependency { get; set; }
public virtual ICollection<Dependency> EndpointDependency { get; set; }
}
}
Upvotes: 1
Reputation: 817
Your namespace of both the partial classes are required to be same. If not, .NET will take it as two different classes and metadata will not work on that. As mentioned by @Rowan Freeman in comment, please make sure that namespace for both the classes are same.
Upvotes: 3