Reputation: 113
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.
VIEW
@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>
}
MODEL
public class UploadFileValidation
{
public string InvoiceNumber { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime InvoiceD { get; set; }
}
Upvotes: 0
Views: 31
Reputation: 4375
Adjusting @foreach (var validateOutput in Model)
will do this:
@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>
}
or
@foreach (var validateOutput in Model)
{
<tr>
<td>@validateOutput.InvoiceNumber </td>
<td>@validateOutput.InvoiceD </td>
<td>@Html.ValidationMessageFor(m => validateOutput.InvoiceD)</td>
</tr>
}
Upvotes: 1