0

ビューから DateTime のコレクションを渡そうとしています:

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

これが私の行動です:

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

しかし、日付では常に null になります。助言がありますか?

アップデート

問題は、間違った URL を作成すると思うことです:

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)
{ ...
4

1 に答える 1

1

申し訳ありませんが、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>

URL は次のようになります /Home/Index?dates[0]=2012-11-12T00:00:00&dates[1]=2013-09-13T00:00:00

于 2013-09-27T16:50:23.403 に答える