Home > front end >  ViewModel not passing information correctly Asp.net Core MVC
ViewModel not passing information correctly Asp.net Core MVC

Time:01-11

I've fairly new to Asp.net core MVC and have spent days researching this and I can't find my error.

I'm simply trying to pass data from a ViewModel to a table in a view but the ViewModel isn't populating data.

Here's my code:

The model I'm currently working with (The others look the same with different columns):

using System;
using System.Collections.Generic;

namespace Seating.Models
{
    public partial class Dth
    {
        public int Id { get; set; }
        public DateTime TimeEntered { get; set; }
        public DateTime? TimeCleared { get; set; }
        public bool? EmpSent { get; set; }
        public int EmployeeId { get; set; }
        public int EmpPosition { get; set; }
        public int? RlfPosition { get; set; }

        public virtual Position EmpPositionNavigation { get; set; }
        public virtual Employee Employee { get; set; }
        public virtual Position RlfPositionNavigation { get; set; }
    }
}

VM:

using Seating.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Seating.ViewModels
{
    public class ListsVM
    {
        public IEnumerable<Dth> Dths { get; set; }
        public IEnumerable<Break> Breaks { get; set; }
        public IEnumerable<Lunch> Lunches { get; set; }
        public IEnumerable<Employee> Employees { get; set; }
        public IEnumerable<Position> Positions { get; set; }
    }
}

Controller:

using Seating.Models;
using Seating.ViewModels;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace Seating.Controllers
{
    public class HomeController : Controller
    {
        private readonly db_a7e17a_seatingContext _context = new db_a7e17a_seatingContext();

        public HomeController(db_a7e17a_seatingContext context)
        {
            _context = context;
        }

        public ActionResult Index()
        {
            var tables = new ListsVM
            {
                Employees = _context.Employees.ToList(),
                Dths = _context.Dths.Where(n => n.TimeCleared == null).ToList(),
                Breaks = _context.Breaks.Where(n => n.TimeCleared == null).ToList(),
                Lunches = _context.Lunches.Where(n => n.TimeCleared == null).ToList(),
                Positions = _context.Positions.ToList()
            };
            return View(tables);
        }

And view:

@model IEnumerable<Seating.ViewModels.ListsVM>

@{
    ViewData["Title"] = "Home Page";
}

<div  style="width: 18rem;">
    <div >
        DTH
    </div>
    <table >
        <tbody>
            @foreach (var item in Model.Dths)
            {
                <tr>
                    @item.EmployeeId
                </tr>
            }
        </tbody>
    </table>
</div>

I'm getting an error saying 'IEnumerable' does not contain a definition for 'Dths'accepting a first argument of type 'IEnumerable' could be found.

I tried changing "IEnumerable" to "List" in the VM. I've read many solutions on stackoverflow. I've taken tutorials that have showed me how to set things up. As far as I can tell I'm following the tutorial code to the letter, even thought I'm clearly not somewhere.

I changed the model reference in the view to @model IEnumerable<Seating.Models.Dth> instead of the VM. That works perfectly. So I'm assuming I'm doing something wrong with the VM since it works referencing the model directly without going through a VM.

Any thoughts on what I'm doing wrong?

CodePudding user response:

You are creating an object of type ListsVM that contains lists of different kinds of entities. And this object is passing to the view by View(tables). But in the view you declared your model as a collection of objects ListsVM: @model IEnumerable<Seating.ViewModels.ListsVM>

Did you try to change the model declaration to @model Seating.ViewModels.ListsVM?

If to declare the view model as @model Seating.ViewModels.ListsVM then the Model.Dths will be referencing to correct collection and the @foreach (var item in Model.Dths) will enumerate this collection properly.

  •  Tags:  
  • Related