繁体   English   中英

ASP.NET MVC从视图到控制器在ActionLink中传递DateTime的数组/集合

[英]ASP.NET MVC Passing array/collection of DateTime in ActionLink from View to Controller

我正在尝试从我的视图传递DateTime的集合:

@Html.ActionLink("Hello", "Index", "Home", new { dates = new Collection(){date1, date2)} })

这是我的动作:

[HttpGet]
public ActionResult Index(int? page, string searchString, ICollection<DateTime> dates)
{ ...

但是我总是约会中没有空。 有什么建议么?

更新

问题是因为我认为它会创建错误的网址:

http://localhost:39152/Home?dates=System.Collections.ObjectModel.Collection%601%5BSystem.DateTime%5D

顺便说一句我补充说:

public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
  var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
  var date = value.ConvertTo(typeof (DateTime), CultureInfo.CurrentCulture);
  return date;
}

并采取行动:

[HttpGet]
public ActionResult Index(int? page, string searchString, [ModelBinder(typeof (ModelBinders.DateTimeBinder))]  ICollection<DateTime> dates)
{ ...

抱歉,我不知道如何使用UrlActionLink或Url.Action进行编码,因为它们将对括号进行编码,但是如果您传递正确的查询字符串,则默认的ModelBinder可以处理ICollection日期,类似这样

    var dates = new[] { new DateTime(2012, 11, 12), new DateTime(2013, 09, 13) };
    <a href="@(Url.Action("Index", "Home"))?@(string.Join("&", dates.Select((date, i) => Url.Encode("dates") + "[" + i + "]=" + date.ToString("s"))))">Hello</a>

网址看起来像这样/ home / Index?dates [0] = 2012-11-12T00:00:00&dates [1] = 2013-09-13T00:00:00

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM