【发布时间】:2019-11-12 13:30:37
【问题描述】:
我的模型中有一个用于 DateRangePickerFor 组件的数组类型 DateTime 属性。 型号:
public class DateRange
{
[Required(ErrorMessage = "Please enter the value")]
public DateTime?[] value { get; set; }
}
执行 post 操作时,模型值采用以下类型。由于此控件默认需要两个数组值,并且由于可以为空类型,因此我得到了以下值。
{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