如何将List <objects>从控制器操作传递到剃刀视图并迭代项目

时间:2019-06-15 19:08:26

标签: asp.net-mvc razor

我有一个控制器操作方法,该方法会将列表返回到剃刀查看页面。 我正在尝试迭代这些项目,但会引发以下错误。

错误CS1061'对象'不包含'ContactName'的定义,并且找不到可以接受的扩展方法'ContactName'接受类型为'object'的第一个参数(您是否缺少using指令或程序集引用?)

public ActionResult GetCompaniesFromUploadedFile()
        {
            List<object> result= null;
            var httpContext = HttpContext.Request;
            string fileName = httpContext.Files[0].FileName;
            if (httpContext.Files.Count != 0 || httpContext.Files[0].ContentLength != 0)
            {
                if (fileName.IsTextFile())
                {
                    var companiesContext = new CompaniesManagementBusinessLogic();
                    result = companiesContext.GetCompanies(httpContext.Files[0].InputStream);
                }
            }
            return View(result);
        }

@model List<object>

@{
    ViewBag.Title = "GetCompaniesFromUploadedFile";
}

<h2>Get Companies From Uploaded File</h2>

<table class="table table-bordered">
    <thead>
        <tr>
            <th>Company Name</th>
            <th>Years in Business</th>
            <th>Contact Name</th>
            <th>Contact Phone Number</th>
            <th>Contact Email</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <th scope="row">@item.CompanyName</th>
                <td>@item.YearsInBusiness</td>
                <td>@item.ContactName</td>
                <td>@item.ContactPhoneNumber</td>
                <td>@item.ContactPhoneNumber</td>
                <td>
                    <a data-value="@item.Id"
                       href="javascript:void(0)" class="btnEdit">Edit</a>
                </td>
            </tr>
        }
    </tbody>
</table>

enter image description here

public List<object> GetCompanies(Stream inputStream)
        {
            //var engine = new FileHelperEngine<FileDataViewModel>(Encoding.UTF8);
            //var engine = new DelimitedFileEngine<FileDataViewModel>();
            //engine.Options.Delimiter = "#";
            //engine.Options.Delimiter = "-";
            //List<FileDataViewModel> companies = engine.ReadStream(new StreamReader(inputStream)).ToList();
            string line;
            string delimiter;
            List<string> list = new List<string>();
            List<object> companies = new List<object>();
            //List<object> hyCompanies = new List<object>();
            //List<HashViewModel> haCompanies = new List<HashViewModel>();

            using (StreamReader reader = new StreamReader(inputStream))
            {
                while ((line = reader.ReadLine()) != null)
                {
                    list.Add(line); // Add to list.                    
                }
            }            
            //int countComma = line.Split(',').Length - 1;
            int countComma = list[0].Split(',').Length - 1;
            //int countHash = line.Split('#').Length - 1;
            int countHash = list[0].Split('#').Length - 1;
            //int countHyphen = line.Split('-').Length - 1;
            int countHyphen = list[0].Split('-').Length - 1;

            //HighestCount = (Comparer 3 counts) 5 , 

            if (countComma > countHash)
            {
                if (countComma > countHyphen)
                {
                    delimiter = ",";
                    //string[] items = source.Split(',');
                    //foreach items to table                    
                    foreach (string row in list)
                    {
                        companies.Add(new CommaViewModel
                        {
                            CompanyName = row.Split(',')[0],
                            ContactName = row.Split(',')[1],
                            ContactPhoneNumber = row.Split(',')[2],
                            YearsInBusiness = Convert.ToInt32(row.Split(',')[3]),
                            ContactEmail = row.Split(',')[4]
                        });
                    }
                }
                else
                {
                    delimiter = "-";
                    //string[] items = source.Split('-');                    
                    foreach (string row in list)
                    {
                        companies.Add(new HyphenViewModel
                        {
                            CompanyName = row.Split('-')[0],
                            YearFounded = Convert.ToInt32(row.Split('-')[1]),
                            ContactPhoneNumber = row.Split('-')[2],
                            ContactEmail = row.Split('-')[3],
                            ContactFirstName = row.Split('-')[4],
                            ContactLastName = row.Split('-')[5],
                        });
                    }
                }
            }
            else if (countHash > countHyphen)
            {
                delimiter = "#";
                //string[] items = source.Split('#');                
                foreach (string row in list)
                {
                    companies.Add(new HashViewModel
                    {
                        CompanyName = row.Split('#')[0],
                        YearFounded = Convert.ToInt32(row.Split('#')[1]),
                        ContactName= row.Split('#')[2],
                        ContactPhoneNumber = row.Split('#')[3]                        
                    });
                }
            }
            else
            {
                delimiter = "-";
                //string[] items = source.Split('-');
                //sort                
                foreach (string row in list)
                {
                    companies.Add(new HyphenViewModel
                    {
                        CompanyName = row.Split('-')[0],
                        YearFounded = Convert.ToInt32(row.Split('-')[1]),
                        ContactPhoneNumber = row.Split('-')[2],
                        ContactEmail = row.Split('-')[3],
                        ContactFirstName = row.Split('-')[4],
                        ContactLastName = row.Split('-')[5],
                    });
                }
            }

            //Console.WriteLine("ananth");
            return companies;
        }

0 个答案:

没有答案