Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法在日期列表的数据批注上的无效值错误消息中获取字段名_C#_Asp.net Mvc_Data Annotations_Modelstate - Fatal编程技术网

C# 无法在日期列表的数据批注上的无效值错误消息中获取字段名

C# 无法在日期列表的数据批注上的无效值错误消息中获取字段名,c#,asp.net-mvc,data-annotations,modelstate,C#,Asp.net Mvc,Data Annotations,Modelstate,我有一个具有类似代码的MVC应用程序。我使用JS在单击按钮时添加日期字段 型号: public class FruitViewModel { public Guid Id { get; set; } public string Name { get; set; } public List<FruitDate> DateList {get;set;} } public class FruitDate { [DataType(DataType.Date

我有一个具有类似代码的MVC应用程序。我使用JS在单击按钮时添加日期字段

型号:

public class FruitViewModel
{ 
    public Guid Id { get; set; }
    public string Name { get; set; }
    public List<FruitDate> DateList {get;set;}
}

public class FruitDate
{
    [DataType(DataType.Date, ErrorMessage = "Invalid Date")]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime BuyDate{get;set;}
    [DataType(DataType.Date, ErrorMessage = "Invalid Date")]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime SellDate{get;set};        
}
它一直运行良好,买卖日期被绑定到模型上。在无效的买入或卖出日期,例如3019年2月31日,模型状态将按预期失败。但是,allErrors变量有一条消息

值“”无效


为什么我没有获取无效字段的字段名,例如值'FruitDate[0].BuyDate'无效。

您没有获取字段名,因为在从post数据绑定到模型之前,
数据类型
属性没有验证

要使
Required
属性起作用,必须将
DateTime
字段更改为空:

public DateTime? BuyDate{get;set;}

public DateTime? SellDate{get;set};

name=“FruitDate[0].SellDate
name=“DateList[0].SellDate
?@NaDeRStar感谢您指出错误。错误仍然存在。我不需要必需的属性。我需要知道哪个字段的日期不好。
$(document).on('click', '#btnSubmit', function (e) {
    let newDiv = '<div class="row"><div class="col-md-12"> +
                 '<div class="col-xs-6"><input class="form-control" type="date" data-val="true" data-val-required="The BuyDate field is required." id="DateList_0__BuyDate" name="DateList[0].BuyDate" value="' + currentdate + '"> </div>' +
                 '<div class="col-xs-6"><input class="form-control" type="date" data-val="true" data-val-required="The SellDate field is required." id="DateList_0__SellDate " name="DateList[0].SellDate " value="' + currentdate + '"></div>' +           
                 '</div ></div >'

 $('#divFruits').append(newDiv);
});
[HttpPost]
public ActionResult SaveFruits(FruitViewModel model)
{
    try
    {
        if (ModelState.IsValid)
        {
        }
         var allErrors = ModelState.Values.SelectMany(v => v.Errors.Select(b => b.ErrorMessage));
         ModelState.AddModelError("", "An error occured. Please try again");
    }
}
public DateTime? BuyDate{get;set;}

public DateTime? SellDate{get;set};