Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3696574
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:45:19+00:00 2026-05-19T04:45:19+00:00

I been reading pro asp.net mvc 2.0 framework and I am a bit confused

  • 0

I been reading pro asp.net mvc 2.0 framework and I am a bit confused what I should use for my validation, where it should go and how to make sure I don’t have to keep writing the same code.

My sites tend to be almost all ajax with jquery. So what I usually did was have jquery.validate for my client side and then on the server side have some checks again. If it would fail on the server side or if I had a validation rule I could not test on the client side then I would return the error messages.

A couple things suck with this way. First I have to make sure that the error messages are the same on the client side and server side. So I will always have 2 duplicate messages.

So if I misspell a word I got to make sure I remember to change it in 2 places. Second it hard to return the server side errors(most of my sites are almost all ajax) so what I did was I always had to check for a flag.

$.post('Create',{'test',test},function(response)
{
    if(response.IsValid == false) 
    {
        // check other json parameters to get all error msgs
        // add them to some div container and display to user.
    }
    else
     {
       // show success msg.
     } 
}):

I was looking at data annotations but I am not sure if they will help me since I am using ajax posts.

Will the client side code still show up if you click a button that is hooked up to an ajax post?

I am also guessing the server side messages will never be shown since does it not depend on looking for the html validation helpers that need a full page render?

I also find them pretty limiting. I know you can write your own but that seems to be alot to write(server side and client side code) especially since I would have to write basically everything jquery validate offers.

Is there library out there that is activity updated that allows you to use data annotations with jquery.validate(including remote that jquery.validate)?

Finally I don’t know where this code should go. The author of the book sort of confused me.

He has

 public class Appointment
    {
        [Required(ErrorMessage = "Please enter your name")] [StringLength(50)]
        public string ClientName { get; set; }

        [DataType(DataType.Date)] [Required(ErrorMessage = "Please choose a date")]
        public DateTime AppointmentDate { get; set; }
    } 

He has basic validation in what seems to be a veiw model. I understand this but what confused me is then in a service class he does basic validation again and business validation.

namespace BookingsExample.Domain.Services
{
    public class AppointmentService
    {
        public static void CreateAppointment(Appointment appt)
        {
            EnsureValidForCreation(appt);
            // To do: Now save the appointment to a database or wherever
        }

        private static void EnsureValidForCreation(Appointment appt)
        {
            var errors = new RulesException<Appointment>();

            if (string.IsNullOrEmpty(appt.ClientName))
                errors.ErrorFor(x => x.ClientName, "Please specify a name");

            if (appt.AppointmentDate < DateTime.Now.Date)
                errors.ErrorFor(x => x.AppointmentDate, "Can't book in the past");
            else if ((appt.AppointmentDate - DateTime.Now.Date).TotalDays > 7)
                errors.ErrorFor(x => x.AppointmentDate, "Can't book more than a week in advance");

            if (appt.ClientName == "Steve" && appt.AppointmentDate.DayOfWeek == DayOfWeek.Saturday)
                errors.ErrorForModel("Steve can't book on weekends");

            if (errors.Errors.Any())
                throw errors;
        }
    }
}

Just because your model layer enforces
its own rules doesn’t mean you have to
stop using ASP.NET MVC’s built-in
validation support. I find it helpful
to think of ASP.NET MVC’s validation
mechanism as a useful first line of
defense that is especially good at
generating a client-side validation
script with virtually no work. It fits
in neatly with the view model pattern
(i.e., having simple view-specific
models that exist only to transfer
data between controllers and views and
do not hold business logic): each view
model class can use Data Annotations
attributes to configure client-side
validation.

But still, your domain layer shouldn’t
trust your UI layer to enforce
business rules. The real enforcement
code has to go into the domain using
some technique like the one you’ve
just seen. *

  • This is from the pro asp.net mvc 2.0 framework book chapter 12.

I can understand sort of why he does this so he can take the service layer and use it in different projects(ie you have some mobile application of your site and you need to use the same business logic).

However it looks kinda redundant that he is writing some of the same messages in 2 places and now he has to update the message in 2 places. I am also not sure why he does not trust the “UI” to do the validation because it is being tested on the server side and that should be safe.

So won’t it be just better to have it all in the service layer then? Or is it better to keep simple required fields in the view models for validation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:45:19+00:00Added an answer on May 19, 2026 at 4:45 am

    Data annotations are the best solution to validate your objects on the server. You can validate them in code behind/controllers, service layer or data access layer.

    Unfortunately there is not an out of the box solution to integrate data annotations and jquery.validate, so you will have to use some custom validation code in client script. If you want to have centralized validation, then you can make ajax calls to a validation service (json) that would validate your Appointment object on the server via Data annotations and return a json result to the client. The response can be a simple boolean value or a more complex object that you can use to build your UI display.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been reading through the C++ FAQ and was curious about the friend
I've been reading a little about temporary tables in MySQL but I'm an admitted
I've been reading through a lot of the rookie Java questions on finalize() and
I've been reading about the new developer-only RC0 for Silverlight, and the fact that
I've been reading a lot about closures and I think I understand them, but
I've been reading that Adobe has made crossdomain.xml stricter in flash 9-10 and I'm
I have been reading the MSDN documentation on subclassing and I have been successful
I've been reading about UpdateFrom, used to update a business object from the request.
I've been reading many a tutorial/article on unmanaged DLLs in C++. For the life
I have been reading about the differences between Table Variables and Temp Tables and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.