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 6608651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:37:56+00:00 2026-05-25T19:37:56+00:00

I have a controller action that takes a DateTime? via the query string as

  • 0

I have a controller action that takes a DateTime? via the query string as part of a post-redirect-get. The controller looks like e.g.

public class HomeController : Controller
{
    [HttpGet]
    public ActionResult Index(DateTime? date)
    {
        IndexModel model = new IndexModel();

        if (date.HasValue)
        {
            model.Date = (DateTime)date;
        }
        else
        {
            model.Date = DateTime.Now;
        }

        return View(model);
    }

    [HttpPost]
    public ActionResult Index(IndexModel model)
    {
        if (ModelState.IsValid)
        {
            return RedirectToAction("Index", new { date = model.Date.ToString("yyyy-MM-dd hh:mm:ss") });
        }
        else
        {
            return View(model);
        }
    }
}

My model is:

public class IndexModel
{
    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd MMM yyyy}")]
    public DateTime Date { get; set; }
}

And the Razor view is:

@model Mvc3Playground.Models.Home.IndexModel

@using (Html.BeginForm()) {
    @Html.EditorFor(m => m.Date);
    <input type="submit" />
}

My problem is two fold:

(1) The date formatting applied on the model using the [DisplayFormat] attribute does not work if the query string contains a value for date.

(2) The value held in the model appears to be overwritten with whatever the query string value contains. E.g. if I set a break point inside my Index GET action method, and manually set the date equal to today say, if the query string contains e.g. ?date=1/1/1, then “1/1/1” is displayed in the textbox (the plan is to validate the date and default it if the query string one isn’t valid).

Any ideas?

  • 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-25T19:37:57+00:00Added an answer on May 25, 2026 at 7:37 pm

    Html helpers first use ModelState when binding so if you ever intend to modify some value which is present in the model state inside a controller action make sure that you remove it from the ModelState first:

    [HttpGet]
    public ActionResult Index(DateTime? date)
    {
        IndexModel model = new IndexModel();
    
        if (date.HasValue)
        {
            // Remove the date variable present in the modelstate which has a wrong format
            // and which will be used by the html helpers such as TextBoxFor
            ModelState.Remove("date");
            model.Date = (DateTime)date;
        }
        else
        {
            model.Date = DateTime.Now;
        }
    
        return View(model);
    }
    

    I must agree that this behavior is not very intuitive but it is by design so people should really get used to it.

    Here’s what happens:

    • When you request /Home/Index there is nothing inside ModelState so the Html.EditorFor(x => x.Date) helper uses the value of your view model (which you have set to DateTime.Now) and of course it applies proper formatting
    • When you request /Home/Index?date=1/1/1, the Html.EditorFor(x => x.Date) helper detects that there is a date variable inside ModelState equal to 1/1/1 and it uses this value, completely ignoring the value stored inside your view model (which is pretty much the same in terms of DateTime value but no formatting is applied of course).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple controller actions that takes an id public ActionResult Get(int? id) {
If i have a Controller Action that may recieve both HTTP GET and HTTP
I have an action-method in a controller that takes requests coming from a variety
I have a UserAccountController that takes routes like this /{username}/{action} . I'd like to
I have a controller method that looks like this: [AcceptGet] public ActionResult Index(SecurityMatrixIndexViewModel model)
I have a controller action that takes an id as a parameter. The restful
I have an HttpPost controller action that takes in a simple form DTO object.
I have a controller action that is being executed by a link that was
I have a controller action that checks this.User.Identity.IsAuthenticated What do you suggest how to
I have a HTML form, and I have a Controller Action that accepts the

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.