[英]Passing dates from view to controller mvc/c#
我有一个使用日期范围过滤器的网页,当用户单击“过滤器”操作链接并且控制器返回带有过滤模型数据的“索引”视图时,所选日期范围将传递回控制器。
视图
@Html.ActionLink("Filter", "FilterDateRange", new { from = Url.Encode(Model.FromDate.ToString()), to = Url.Encode(Model.ToDate.ToString()) }, null)
调节器
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);
}
我的问题是,有更优雅的方式来做到这一点吗? 目前日期值正在视图中进行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);
}
如果你让模型绑定器做它的事情,你不必担心编码,解码或解析。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.