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

  • SEARCH
  • Home
  • 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 8130155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:36:17+00:00 2026-06-06T08:36:17+00:00

Possible Duplicate: MVC3 custom validation: compare two dates I am trying to make a

  • 0

Possible Duplicate:
MVC3 custom validation: compare two dates

I am trying to make a validation for my date so that start date must be < than end date.If end date is before start date tha n i throw an exception .How can i show this exception on my screen so when i press search buton and the dates are wrong a message to appear?

    public ActionResult SearchFree(DateTime? StartDate, DateTime? EndDate)
    {

        if (StartDate.HasValue && EndDate.HasValue)
        {
        DateTime d1 = StartDate.Value;
        DateTime d2 = EndDate.Value;
        TimeSpan span = d2 - d1;


        if (span.Days <= 0)
        {
            throw new ArgumentOutOfRangeException("start date must be before end date");
        }

        try
        {
            DBContext.Current.Open();
            var model = Reservation.SelectFreeRooms(StartDate, EndDate);
            DBContext.Current.Close();
            return View(model);
        }
        catch (ArgumentException ae)
        {
            throw ae;
        }


              }
      return View(new List<dynamic>());
    }

enter image description here

  • 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-06-06T08:36:18+00:00Added an answer on June 6, 2026 at 8:36 am
    public ActionResult SearchFree(DateTime? StartDate, DateTime? EndDate)
    {
    
        if (!StartDate.HasValue || !EndDate.HasValue)
        {
            ModelState.AddModelError("Date", "Dates are empty");
            return View();
        }
    
        if(StartDate.Value > EndDate.HasValue
        {
            ModelState.AddModelError("Date", "start date must be before end date");
            return View();
        }
    
        try
        {
            DBContext.Current.Open();
            var model = Reservation.SelectFreeRooms(StartDate, EndDate);
            DBContext.Current.Close();
            return View(model);
        }
        catch ()
        {
            ModelState.AddModelError("", "Db problem");
            return View();
        }
    }
    

    But the best way – using some model and validate it using attributes

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

Sidebar

Related Questions

Possible Duplicate: How to put span element inside ActionLink MVC3? How to create custom
Possible Duplicate: Emitting unencoded strings in a Razor view I have a string that
Possible Duplicate: thread with multiple parameters How does one thread a sub with two
Possible Duplicate: Session variable is lost on RedirectToAction in IE I'm using MVC3 and
Possible Duplicate: Uploadify: show error message from HTTP response I am trying to get
Possible Duplicate: Schedule Controls for ASP.Net MVC I have MVC3 in which I want
Possible Duplicate: How do I make a list with checkboxes in Java Swing? I
Possible Duplicate: CheckboxList in MVC3 View and get the checked items passed to the
Possible Duplicate: ASP.NET MVC on IIS 7.5 I am trying to deploy a basic
Possible Duplicate: Split A4 PDF page into two A5 and back again I have

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.