I have the following view model
public class FormViewModel
{
[Required]
public DateTime? LocalFrom { get; set; }
[Required]
public DateTime? LocalTo { get; set; }
}
then in the controller I have the following action
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index([Bind(Include = "LocalFrom,LocalTo")] FormViewModel model)
{
if (ModelState.IsValid)
{
var presenter = new ManagePresenter(model);
return View(presenter);
}
return View(model); // here model.LocalFrom, model.LocalTo should be null
}
Here is my HTML form
<form class="form-horizontal" action="/Manage/Index" method="post">
@Html.AntiForgeryToken()
<div class="form-group">
<label class="control-label col-sm-2" for="LocalFrom">From</label>
<div class="col-sm-10">
<input type="datetime" class="form-control" id="LocalFrom" name="LocalFrom" value="@Model.LocalFrom" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="LocalTo">To</label>
<div class="col-sm-10">
<input type="datetime" class="form-control" id="LocalTo" name="LocalTo" value="@Model.LocalTo)" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
I am not sure if making nullable property required is logical or not here. I basically want to populate the input with the name LocalFrom
in the view with blank
when Model.LocalFrom
is null or with the correct value when Model.LocalFrom
is not null.
Problem
Then ModelState.IsValid
is always returning false even we all the required values are present and valid.
When I make LocalFrom
and LocalTo
not nullable I the ModelState
becomes valid. but in the view will display the time of the beginning of time like so
How can I pass null value to the view and make the ModelState to validate correctly?