Rob*_*son 5 c# asp.net-mvc datetime razor
我有一个使用日期范围过滤器的网页,当用户单击"过滤器"操作链接并且控制器返回带有过滤模型数据的"索引"视图时,所选日期范围将传递回控制器.
视图
@Html.ActionLink("Filter", "FilterDateRange", new { from = Url.Encode(Model.FromDate.ToString()), to = Url.Encode(Model.ToDate.ToString()) }, null)
Run Code Online (Sandbox Code Playgroud)
调节器
public ActionResult FilterDateRange(string from, string to)
{
var fromDate = DateTime.Parse(HttpUtility.UrlDecode(from));
var toDate = DateTime.Parse(HttpUtility.UrlDecode(to));
//do stuffs
return View("Index", statsPages);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,有更优雅的方式来做到这一点吗?目前日期值正在视图中进行url编码,然后在控制器中进行url解码,我宁愿控制器中的方法采用日期时间参数而不是字符串,它看起来有点像hacky.
谢谢.
为什么不只使用 DateTime 参数?
@Html.ActionLink("Filter", "FilterDateRange", new { from = Model.FromDate, to = Model.ToDate }, null)
public ActionResult FilterDateRange(DateTime from, DateTime to)
{
var fromDate = from;
var toDate = to;
//do stuffs
return View("Index", statsPages);
}
Run Code Online (Sandbox Code Playgroud)
如果让模型绑定器完成它的工作,您就不必担心编码、解码或解析。