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?