验证从文本文件中的集合发送的对象 [英] Validate object that is sent from a collection in a text file

查看:87
本文介绍了验证从文本文件中的集合发送的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试验证文件中的日期字段,以确保它使用的是MM/dd/yyyy字段.我希望错误消息出现在实际发生错误的文件中的发票旁边.为了将发票编号和日期放在一个表中,我必须将它们作为集合发送,以便可以循环列表并显示它们.由于数据是作为列表发送的,因此我必须在视图中指定 IList< WebApplication2.Models.UploadFileValidation> .这意味着当我在表中放入 @ Html.ValidationMessageFor(m => m.InvoiceD)时,显然它无法找到InvoiceD,因为它不存在.

I am trying to validate the date field in my file to make sure that it's using the MM/dd/yyyy field. I want the error message to appear next to the invoice in the file that the error actually occurs. In order to put my invoice numbers and dates in a table, I had to send them as a collection so that I could loop the list and display them. Since the data is sent as a list, I had to specify IList<WebApplication2.Models.UploadFileValidation> in my view. That means when I put @Html.ValidationMessageFor(m => m.InvoiceD) in my table, it obviously won't be able to find InvoiceD because it won't exist.

任何帮助将不胜感激,因为这对我来说是新的领域.下面是我的完整代码.

Any help would be appreciated, as this is delving into new waters for me. Below is my full code.

查看

@model IList<WebApplication2.Models.UploadFileValidation>
        @foreach (var validateOutput in Model)
        {
            <tr>
                <td>@validateOutput.InvoiceNumber </td>
                <td>@validateOutput.InvoiceD </td>
                <td>@Html.ValidationMessageFor(m => m.InvoiceD)</td>
            </tr>
        }

模型

public class UploadFileValidation
{
    public string InvoiceNumber { get; set; }
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
    public DateTime InvoiceD { get; set; }
}

推荐答案

调整 @foreach(模型中的var validateOutput)将执行以下操作:

@for (var i = 0; i < Model.Count(); i++)
{
    <tr>
        <td>@Model[i].InvoiceNumber </td>
        <td>@Model[i].InvoiceD </td>
        <td>@Html.ValidationMessageFor(m => m[i].InvoiceD)</td>
     </tr>
}

@foreach (var validateOutput in Model)
{
    <tr>
       <td>@validateOutput.InvoiceNumber </td>
       <td>@validateOutput.InvoiceD </td>
       <td>@Html.ValidationMessageFor(m => validateOutput.InvoiceD)</td>
    </tr>
}

这篇关于验证从文本文件中的集合发送的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆