如何将变量放入列表内的对象

时间:2019-05-22 08:55:22

标签: c# asp.net-mvc

因此,我正在创建一个表以输出数据库数据的值。在此过程中,我只能输出第一条记录。然后我搜索了它,发现必须做一个列表来存储所有数据,现在我陷入了困境。我敢肯定这很简单,但我只是开始

我想这样做:

List<EmployeeViewModel> EmployeeVM = new List<EmployeeViewModel>
{
    MovieID = movie.MovieID,
    MovieName = movie.MovieName,
    MovieDescription = movie.MovieDescription,
    MoviePrice = movie.MoviePrice,
    MovieCategory = movie.MovieCategory,
    MovieYear = movie.MovieYear
};

return View(EmployeeVM);

以便我可以这样输出:

<table class="table table-bordered table-responsive table-hover">
    <tr>
        <th><b>Movie Name </b></th>
        <th><b>Movie Category </b></th>
        <th><b>Movie Year </b></th>
        <th><b>Movie Price</b></th>

    </tr>
    @foreach (EmployeeViewModel item in EmployeeVM)
    {
        <tr>
            <td>@Model.MovieName</td>
            <td>@Model.MovieCategory</td>
            <td>@Model.MovieYear
            <td>@Model.MoviePrice</td>

        </tr>
    }
</table>
  

“列表”不包含“电影名称”的定义

     

“找不到类型或名称空间名称'EmployeeViewModel'(您是否缺少using指令或程序集引用?)”

4 个答案:

答案 0 :(得分:2)

如果您使用的是foreach循环,则应使用item对象。

@foreach (EmployeeViewModel item in EmployeeVM)
{
    <tr>
        <td>@item.MovieName</td>
        <td>@item.MovieCategory</td>
        <td>@item.MovieYear
        <td>@item.MoviePrice</td>
    </tr>
}

答案 1 :(得分:1)

您的代码中有两个不同的问题。

  1. “列表”不包含“电影名称”的定义

    • 这里有EmployeeViewModel的列表,因此您需要向列表中添加EmployeeViewModel的实例。

类似的东西

List<EmployeeViewModel> EmployeeVM = new List<EmployeeViewModel>

foreach(var movie in movies)
{
  var temp =     new EmployeeViewModel {
        MovieID = movie.MovieID,
        MovieName = movie.MovieName,
        MovieDescription = movie.MovieDescription,
        MoviePrice = movie.MoviePrice,
        MovieCategory = movie.MovieCategory,
        MovieYear = movie.MovieYear
    };

EmployeeVM.Add(temp);
}

如果您只有Movie的一条记录,则直接使用Employee的单视图模型

  1. “找不到类型或名称空间名称'EmployeeViewModel'(您是否缺少using指令或汇编引用?)”

对于此问题,您需要在定义EmployeeViewModel的位置导入库。

类似

using Stackoverflow.Sample.EmployeeViewModel;

其中Stackoverflow.Sample是您的项目,EmployeeViewModel是您的课程

答案 2 :(得分:0)

您应在列表初始化程序内创建EmployeeViewModel的实例:

List<EmployeeViewModel> EmployeeVM = new List<EmployeeViewModel>
{
    new EmployeeViewModel {
        MovieID = movie.MovieID,
        MovieName = movie.MovieName,
        MovieDescription = movie.MovieDescription,
        MoviePrice = movie.MoviePrice,
        MovieCategory = movie.MovieCategory,
        MovieYear = movie.MovieYear
    },
    ///other list items
};

此外,您应该在每次迭代中使用@item(而不是@Model):

@foreach (EmployeeViewModel item in Model)
{
    <tr>
        <td>@item.MovieName</td>
        <td>@item.MovieCategory</td>
        <td>@item.MovieYear
        <td>@item.MoviePrice</td>
    </tr>
}

答案 3 :(得分:0)

您的列表是 EmployeeViewModel 的列表,因此您需要像下面进行初始化

List<EmployeeViewModel> EmployeeVM = new List<EmployeeViewModel>
        {   new EmployeeViewModel()
            {
                MovieID = movie.MovieID,
                MovieName = movie.MovieName,
                MovieDescription = movie.MovieDescription,
                MoviePrice = movie.MoviePrice,
                MovieCategory = movie.MovieCategory,
                MovieYear = movie.MovieYear
            }
        };