0

So I'm creating a table to output the values of my database's data. During the process, I was able to output, but only the first record. Then I searched for it and noticed that I had to do a list to store all the data and now I'm stuck. I'm pretty sure it's simple, but I'm only starting

I wanted to do this:

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);

so that i can output like this:

<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>

"List" does not contain a definition for "MovieName"

"The type or namespace name 'EmployeeViewModel' could not be found (are you missing a using directive or an assembly reference?)"

Tom Halson
  • 380
  • 3
  • 12
Diogo Teixeira
  • 91
  • 1
  • 12

4 Answers4

1

Your List is a list of EmployeeViewModel, so you need to initialize like the below

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
            }
        };
Thangadurai
  • 2,573
  • 26
  • 32
1

if you are using foreach loop so you should use item object.

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

You have two different issues in your code.

  1. "List" does not contain a definition for "MovieName"

    • Here you have list of EmployeeViewModel, so you need to add instances of EmployeeViewModel to the list.

something like,

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);
}

If you have only one record of Movie then directly use single view model of Employee

  1. "The type or namespace name 'EmployeeViewModel' could not be found (are you missing a using directive or an assembley reference?)"

For this issue you need to import library where you defined EmployeeViewModel.

something like

using Stackoverflow.Sample.EmployeeViewModel;

where Stackoverflow.Sample is your project and EmployeeViewModel is your class

Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
  • well, the second part it doesnt, because it is in the cshtml file. It still has errors in the foreach statement – Diogo Teixeira May 22 '19 at 10:19
  • if it is in .cshtml then try `@using Stackoverflow.Sample.EmployeeViewModel;`. i.e. append `@` before using. please let me know any further issue you are facing – Prasad Telkikar May 22 '19 at 10:23
  • It says that the using directive is unnecessary – Diogo Teixeira May 22 '19 at 10:29
  • @foreach (EmployeeViewModel item in EmployeeVM) { @item.MovieName @item.MovieCategory @item.MovieYear @item.MoviePrice } At the foreach statement, the EmployeeViewModel and the EmployeeVM has errors, saying that it doesnt exist. Ive tried with var and Model but it was only outputting 1 record – Diogo Teixeira May 22 '19 at 10:35
  • `EmployeeViewModel` and `EmployeeVM` are coming from .cs file you need to include class namespace into .cshtml using `@using Stackoverflow.Sample.EmployeeViewModel;`.. this will resolve your problem – Prasad Telkikar May 22 '19 at 10:40
  • Now it outputs this error "The type or namespace 'EmployeeViewModel' does not exist in the namespace 'WebApplication3.Models '(are you missing an assembly reference?)". Do I need to put another @using in the .cs file? – Diogo Teixeira May 22 '19 at 10:46
  • This might helpful for you https://stackoverflow.com/q/40758885/6299857, If not then just google it or add new question – Prasad Telkikar May 22 '19 at 10:51
0

You should create instances of EmployeeViewModel inside list initializer:

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
};

Also you should use @item(not @Model) inside each iteration:

@foreach (EmployeeViewModel item in Model)
{
    <tr>
        <td>@item.MovieName</td>
        <td>@item.MovieCategory</td>
        <td>@item.MovieYear
        <td>@item.MoviePrice</td>
    </tr>
}
Roman Koliada
  • 4,286
  • 2
  • 30
  • 59
  • Thank you so much. Now im getting 2 errors in the second part of the problem. When I try to output, in the foreach statement – Diogo Teixeira May 22 '19 at 09:11