DateTime数组类型的服务器端数据注释不能正常工作

问题描述 投票:0回答:1

我的模型中具有DateRangePickerFor组件的数组类型DateTime属性。型号:

public class DateRange
{
    [Required(ErrorMessage = "Please enter the value")]
    public DateTime?[] value { get; set; }
}

执行后期动作时,模型值采用以下类型。由于此控件默认需要两个数组值,并且由于可为空类型,因此下面的值是我得到的。

{System.DateTime?[2]}
[0]: null
[1]: null

因此,在这种情况下,将值设置为value [null,null],并且未设置给定的注释“请输入值”。因此,任何人都可以建议处理DateTime数组类型并使Data注释在这种情况下有效的方法。控制器:

  public IActionResult Index()
  {
      return View();
  }

  [HttpPost]
  public IActionResult Index(DateRange model)
  {
      //posted value is obtained from the model
      DateRangeValue.value = model.value;
      return View(DateRangeValue);
  }

查看:

<form method="post">
    // Here i use my custom component (i.e) DateRangerPicker which takes the two date array values and displays like this "3/3/2020 - 9/3/2021"
    @Html.DateRangePickerFor(m => m.value)

    <input type="submit" value="Save Student Details" />
</form>
c# asp.net-core data-annotations html-helper
1个回答
0
投票

我建议修改您的自定义组件,使其具有两个单独的属性,而不是类型为DateTime?的二维数组。您的数组本身不为null,只是其两个元素的值,因此您不会收到错误消息。

© www.soinside.com 2019 - 2024. All rights reserved.