I am attempting to display a form to the user to input two date ranges and return the results within the selected date ranges. I'm using a DateTime.cshtml EditorTemplate to handle DateTime values for the date ranges. The nullable DateTime causes an "InvalidOperationException:
Nullable object must have a value."
ViewModel: lloks like this:
public class SearchFilterModel
{
[DisplayName("Date From")]
public DateTime? DateFrom { get; set; }
[DisplayName("Date To:")]
public DateTime?DateTo { get; set; }
Whevenver I am trying to submit the form highlights the date fields in RED and when debug the code the dateFrom and DateTo are always returning null values.
AnithaSurend...
0 Points
3 Posts
Nullable DateTime
May 03, 2012 02:23 PM|LINK
I am attempting to display a form to the user to input two date ranges and return the results within the selected date ranges. I'm using a DateTime.cshtml EditorTemplate to handle DateTime values for the date ranges. The nullable DateTime causes an "InvalidOperationException: Nullable object must have a value."
ViewModel: lloks like this:
public class SearchFilterModel
{
[DisplayName("Date From")]
public DateTime? DateFrom { get; set; }
[DisplayName("Date To:")]
public DateTime?DateTo { get; set; }
}
View Looks like this:
@model Parliament.Oasis.WebManagement.Models.SearchModel
@using (Html.BeginForm("Search", "Document", FormMethod.Get))
{
<section id="searchCriteria">
<header>
<h2>
Search Criteria</h2>
</header>
<div>
@Html.LabelFor(model => model.SearchFilter.DateFrom)
@Html.EditorFor(model => model.SearchFilter.DateFrom})
</div>
<div>
@Html.LabelFor(model => model.SearchFilter.DateTo)
@Html.EditorFor(model => model.SearchFilter.DateTo)
</div>
</section>
}
DateTime.cshtml Looks like this:
@model System.DateTime?
@Html.TextBox(String.Empty, (Model.HasValue ? Model.Value.ToShortDateString() : String.Empty), new{@class="dateTime"})
Whevenver I am trying to submit the form highlights the date fields in RED and when debug the code the dateFrom and DateTo are always returning null values.
How can I handle this correctly?
raduenuca
All-Star
24675 Points
4250 Posts
Re: Nullable DateTime
May 03, 2012 02:40 PM|LINK
Please show the action as well
Radu Enuca | Blog
AnithaSurend...
0 Points
3 Posts
Re: Nullable DateTime
May 03, 2012 02:45 PM|LINK
SearchModel object looks like this:
public class SearchModel
{
public SearchFilterModel SearchFilter { get; set; }
public IList<DocumentModel> SearchResults { get; set; }
public SearchModel()
{
SearchFilter = new SearchFilterModel();
}
}
Action Looks something like this:
public ActionResult Search(SearchModel searchModel)
{
searchModel.SearchResults =
DocumentRepository.FindResults( searchModel.SearchFilter.DateFrom, searchModel.SearchFilter.DateTo).
ToList();
return View("Index", searchModel);
}
AnithaSurend...
0 Points
3 Posts
Re: Nullable DateTime
May 03, 2012 03:31 PM|LINK
Removed the FormMethod.Get from the @Html.BeginForm, it is working now.
I don't know the why FormMethod.Get causing problem but solved my problem just removing that.