簡體   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