'IEnumerable <>'不包含''的定义,也没有扩展方法''接受类型为'IEnumerable <>

时间:2019-03-07 20:34:40

标签: c# asp.net asp.net-mvc

完全错误

  

'IEnumerable '不包含以下定义   “ InvoiceDate”,没有扩展方法“ InvoiceDate”接受第一个   可以找到类型为“ IEnumerable ”的参数(您是   缺少using指令或程序集引用?)

我目前正在试图弄清楚为什么会出现此错误。我将模型指定为IEnumerable,并且正在循环列表。任何帮助将不胜感激。

修复此问题后的最终结果应该是我可以上传文件,并且它将在数组中的第二个元素中搜索不支持格式的发票日期。然后它将在发票号旁边输出validationMessage。

查看

@using UploadData.Models
@model IEnumerable<UploadFileData>
<html>
<body>
@using (@Html.BeginForm("Index", "ValidateUpload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="validateFile" /><br />
    <input type="submit" value="Upload" name="Submit" />
}
@if (Model.Count() > 0)
{
    <hr />
    <table id="counttable">
        <tr>
            <th rowspan="1">Invoice #</th>
            <th rowspan="1">Error</th>
        </tr>
        @foreach (UploadFileData ValidationOutput in Model)
            {
            <tr>
                <td>@ValidationOutput.Invoice</td>
                <td>@Html.ValidationMessageFor(m => m.InvoiceDate, "", new { @class = "text-danger" })</td>
            </tr>
        }
    </table>
}
</body>
</html>

型号

using System;
using System.Globalization;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace UploadData.Models
{
public class UploadFileData : IValidatableObject
{
    [Required]
    public string Invoice { get; set; }
    [Required]
    public string InvoiceDate { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        DateTime date;
        var property = new[] { "InvoiceDate" };
        string[] formats = {"MM/d/yyyy", "M/dd/yyyy"};
        if (!DateTime.TryParseExact(InvoiceDate, formats, null, DateTimeStyles.None, out date))
        {
            yield return new ValidationResult("Invalid date format. Dates should be M/d/yyyy OR MM/d/yyyy", property);
        }
    }
}
}

控制器

using System;
using System.IO;
using System.Web;
using System.Web.Mvc;
using System.Globalization;
using System.Collections.Generic;
using System.Linq;
using UploadData.Models;

namespace WebApplication2.Controllers
{

public class ValidateUploadController : Controller
{
    // GET: ValidateUpload
    public ActionResult Index()
    {
        return View(new List<UploadFileData>());
    }
    [HttpPost]
    public ActionResult Index(HttpPostedFileBase validateFile)
    {
        List<UploadFileData> ValidateOutput = new List<UploadFileData>();
        if (validateFile != null)
        {
            string idate = string.Empty;
            string invoice = string.Empty;
            int namecount = 1;
            string dir = Server.MapPath("~/sourcefiles/");
            string strbuild = string.Empty;
            string fileName = Path.GetFileName(validateFile.FileName);
            string fileNameOnly = Path.GetFileNameWithoutExtension(validateFile.FileName);
            string newFullPath = Path.Combine(Server.MapPath("~/textfiles"), fileName);

            while (System.IO.File.Exists(newFullPath))
            {
                string tempFileName = string.Format("{0}({1})", fileNameOnly, namecount++);
                newFullPath = Path.Combine(Server.MapPath("~/textfiles"), tempFileName + Path.GetExtension(fileName));
            }
            validateFile.SaveAs(newFullPath);
            using (StreamReader sr = new StreamReader(newFullPath))
            {
                while ((strbuild = sr.ReadLine()) != null)
                {
                    string[] strArray = strbuild.Split('|');

                    if (strArray[0] == "1")
                    {
                        invoice = strArray[1];
                        idate = strArray[2];

                        ValidateOutput.Add(new UploadFileData
                        {
                            Invoice = invoice,
                            InvoiceDate = idate,
                        });
                    }
                }
            }
        }
        return View(ValidateOutput);
    }
}
}

1 个答案:

答案 0 :(得分:0)

我想我明白了。在这里:

@Html.ValidationMessageFor(m => m.InvoiceDate, "", new { @class = "text-danger" })

正如Mohsin Mehmood所建议的,m属于IEnumerable<UpdloadFileData>,不具有InvoiceDate属性。您试图获取单个元素的属性,但要引用其“容器”(IEnumerable)。您需要做的是

@Html.ValidationMessageFor(m => m.Where(x => x.InvoiceDate == ValidationOutput.InvoiceDate), "", new { @class = "text-danger" })