我正试图将两个日期参数从View传递到Controller。
查看
<div class="col-md-4">
<p>Date From:</p>
@Html.TextBox("ExpDateFrom", ViewBag.ExpDateFrom as DateTime)
</div>
<div class="col-md-4">
<p>Date To:</p>
@Html.TextBox("ExpDateTo", ViewBag.ExpDateTo as DateTime)
<input type="submit" value="Find" />
</div>
控制器
public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page, DateTime dateOfExposureFrom, DateTime dateOfExposureTo)
显然,现在这是行不通的。我甚至不确定是否应该使用Html.TextBox,但即使可以,使用DateTime也有问题,因为它不可以为null。
我该如何处理?如果我需要彻底修改我的方法,那没关系。我只需要将两个日期参数从视图传递给控制器。
使用可重用模型-这将在进一步开发过程中使事情变得更容易。
public class SearchModel
{
public string SortOrder { get; set; }
public string CurrentFilter { get; set; }
public string SearchString { get; set; }
public int? Page { get; set; }
public DateTime DateOfExposureFrom { get; set; }
public DateTime DateOfExposureTo { get; set; }
}
然后控制器
public ActionResult Index(SearchModel model)
然后查看
@model SearchModel
<div class="col-md-4">
<p>Date From:</p>
@Html.TextBoxFor(m => m.DateOfExposureFrom, new { @class = "form-control" })
</div>