0

When I create a new MVC3 application, add a model, add a [Required] attribute to the model, scaffold a new edit view for this model, and add a @Html.ValidationMessageFor(...), everything works beautifully. If I attempt to submit the edit form without filling in a value it gives me a validation message.

My problem is that this is not working in the enterprise app I'm working on. Here's what I've done and I wonder if you can think of anything else I can check to troubleshoot. I can't share the code, but perhaps you can see an obvious omission from the list...

  • Add [Required] attribute to my model's property
  • Make sure jquery.validate.js and jquery.validate.unobtrusive.js are referenced
  • Added Html.EnableClientValidation(true) and Html.EnableUnobtrusiveJavaScript(true) on my view
  • Added my @Html.ValidationMessageFor(...) next to my control (which is a @Html.TextBoxFor)

The issue is that I can submit the form and no validation is performed. I get a server-side error stating that the entity is not valid, which I would expect if it gets past the client-side validation.

When I inspect the source, it has not rendered the validation attributes to my textbox. I would expect it to look something like this...

<input class="text-box single-line" data-val="true" 
    data-val-required="The Property1 field is required." 
    id="Property1" name="Property1" type="text" value="" />

But instead it looks more like this...

<input id="Reminder_Description" name="Reminder.Description" 
    style="width:300px;" type="text" value="" />

What exactly is responsible for injecting those attributes?

Thanks!

3
  • Any javascript console errors? Commented Jan 27, 2012 at 21:31
  • 1
    Are you creating your form with @Html.BeginForm? Does the call to Html.EnableClientValidation(true) precede this? Commented Jan 27, 2012 at 21:48
  • Please show your Get and Post methods. Commented Jan 27, 2012 at 22:02

2 Answers 2

1

I figured out the issue. I just didn't know that attributes are not passed through a service layer (WCF), so the Required attribute was not effective. To remedy, we added a local reference to the entities assembly (which makes sense in our architecture). Thanks.

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

3 Comments

I hardly figure out how this makes sense... Could you explain a bit?
@fredlegrain, the attributes that I add to my business objects are obviously in a completely separate assembly from the web application that I'm using them in. I have a service project that is referencing the business object assembly and ultimately serializing the objects and passing them over the wire. It makes sense that attributes would not be passed along. But if I add a ref. to the business objects in the web app then I get those attributes. Does that make sense?
I took a deeper look at DataAnnotations and understood the sense it makes. Thanks.
0

Have you checked your web.config for:

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

2 Comments

I'm not sure if Html.EnableClientValidation(true) and Html.EnableUnobtrusiveJavaScript(true) on your view has the same effect as setting it up in your web.config. Might be worth trying though.
I did make sure those were on. Thanks.

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.