When I first load the page both my date textboxes are failing validation. This appears to be because the two Date properties are set as required but are null.
My goals are to:
1) Have a model to pass into the controller that contains the criteria to search for.
2) That criteria will get reloaded when the page returns along with the results.
3) When the page 1st loads it will set the dates to a default to DateTime.Now
and NOT show any results. When you submit the criteria it will then show results on next page load.
// Model
public class SearchModel
{
public long? StudentId { get; set; }
[Required]
public DateTime? Date1 { get; set; }
[Required]
public DateTime? Date2 { get; set; }
public List<string> Students { get; set; }
}
// View
@model SearchModel
<div>
@using (Html.BeginForm("StudentSearch", "Student", FormMethod.Post))
{
<span>
Date 1 @Html.TextBoxFor(m => m.Date1)
Date 2 @Html.TextBoxFor(m => m.Date2)
<input type="submit" value="Search" />
</span>
}
</div>
<div>
@foreach(var s in model.Students)
{ <span>@s</span> }
</div>
// Controller
[HttpGet]
public ActionResult StudentSearch(SearchModel model)
{
if (model.Date1 == null || model.Date2 == null)
{
model.Date1 = DateTime.Now;
model.Date2 = DateTime.Now;
}
return View();
}