Home > Software design >  ASP.NET Core MVC frontend development List show View keep getting System.Collections.Generic.List
ASP.NET Core MVC frontend development List show View keep getting System.Collections.Generic.List

Time:03-31

I'm trying to display data from three models, have two models(STAFF、DEPARTMENT) using show data, make Cascading Dropdown one model(STAFF_PERMISSION) insert data,make

but keep getting: The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List1[Voyager.Models.Department.Staff_To_Show]

StaffPermission.cshtml

@model Voyager.Models.Department.Staff_To_Show
    
    @{
        ViewBag.Title = "new";
        
    }
    
    <h2>new</h2>
    
    <form  method="post" action="/Home/StaffPermission">
    
        @Html.AntiForgeryToken()
      
        <div >
    
       <select id="DepartmentNumber" name="DepartmentNumber">
                <option value="">select</option>
    
                  @foreach (var item in Model.Departments)
        {
               
            <option value="@item.DepartmentNumber"> @Html.DisplayFor(modelItem => item.DepartmentNumber)</option>
    
        }
               
            </select>
    <br/>
       <select id="DepartmentNumber" name="DepartmentNumber">
                <option value="">select</option>
    
                  @foreach (var item in Model.Staffs)
        {
               
            <option value="@item.StaffNumber"> @Html.DisplayFor(modelItem => item.StaffNumber)</option>
    
        }
               
            </select>
            
            <hr />
                  <div >
                @Html.LabelFor(model => model.StaffPermissions, htmlAttributes: new { @class = "control-label col-md-2" })
                <div >
                    @Html.EditorFor(model => model.StaffPermissions, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.StaffPermissions, "", new { @class = "text-danger" })
                </div>
            </div>
    
     
    
            <div >
                <div >
                    <input type="submit" value="add"  />
                </div>
            </div>
        </div>
      </form>

HomeController.cs

 [HttpGet("[action]")]
    public ActionResult StaffPermission()
    {
        var query = dbContext.staff.ToList();
        var queryDepartment = dbContext.Departments.ToList();

        var model = new List<Staff_To_Show>();

        List<Department> dataDepartment = new List<Department>();
        dataDepartment.AddRange(queryDepartment);

        List<staff> datastaff = new List<staff>();
        datastaff.AddRange(query);

        model.Add(new Staff_To_Show() { Departments = dataDepartment });
        model.Add(new Staff_To_Show() { Staffs = datastaff });


        return View(model);
    }

    [HttpPost]
    public ActionResult StaffPermission(String DepartmentNumber, String StaffNumber, string AesUpd)
    {

        if (string.IsNullOrWhiteSpace(DepartmentNumber))
        {
            ModelState.AddModelError("Department", "error");
            return View();
        }
        else
        {

            string[] password = getTool.Create(AesUpd);

            StaffPermission addstaff = new StaffPermission();

            addstaff.AesUpd = password[0];
            addstaff.AesKey = password[1];
            addstaff.AesIv = password[2];
            addstaff.LastChangePasswordTime = DateTime.Now;
            addstaff.CreateDate = DateTime.Now;
            addstaff.LastUpd = password[0];
            addstaff.LastKey = password[1];
            addstaff.LastIv = password[2];
            addstaff.IsEnabled = true;
            addstaff.StaffNumber = StaffNumber;
            addstaff.DepartmentNumber = Convert.ToInt16(DepartmentNumber);

            dbContext.StaffPermissions.Add(addstaff);
            dbContext.SaveChanges();

            return View();

        }

    }


StaffToShow.cs

    public partial class Staff_To_Show
    {

        public List<Department> Departments { get; set; } = null!;
        public List<staff> Staffs { get; set; } = null!;
        public StaffPermission StaffPermissions { get; set; } = null!;
    }

StaffPermission.cs

using System;
using System.Collections.Generic;

namespace Voyager.Models.Department
{
    public partial class StaffPermission
    {
        public int DepartmentNumber { get; set; }
        public string StaffNumber { get; set; } = null!;
        public string AesUpd { get; set; } = null!;
        public string AesKey { get; set; } = null!;
        public string AesIv { get; set; } = null!;
        public DateTime? CreateDate { get; set; }
        public string? LastUpd { get; set; }
        public string? LastKey { get; set; }
        public string? LastIv { get; set; }
        public DateTime? LastChangePasswordTime { get; set; }
        public bool? IsEnabled { get; set; }
        public virtual staff StaffNumberNavigation { get; set; } = null!;
    }
}

staff.cs

 public partial class staff
    {
        public int DepartmentNumber { get; set; }
        public string StaffNumber { get; set; } = null!;
        public string? Name { get; set; }
        public string? Phone { get; set; }
        public string? Address { get; set; }
        public string? Email { get; set; }
        public bool? IsEnabled { get; set; }
        public DateTime RecDate { get; set; }

        public virtual Department DepartmentNumberNavigation { get; set; } = null!;
        public virtual StaffPermission StaffPermission { get; set; } = null!;
    }

Department.cs

    public partial class Department
    {
        public Department()
        {
            staff = new HashSet<staff>();
        }

        public int DepartmentNumber { get; set; }
        public string? DepartmentName { get; set; }

        public virtual ICollection<staff> staff { get; set; }
    }

Any help would be appreciated

CodePudding user response:

Your view expects a model of type Staff_To_Show but in your HttpGet Method StaffPermission you create and pass a model of type List<Staff_To_Show>. So your method should look like this:

[HttpGet("[action]")]
public ActionResult StaffPermission()
{
    var datastaff = dbContext.staff.ToList();
    var dataDepartment = dbContext.Departments.ToList();

    var model = new Staff_To_Show {
        Departments = dataDepartment,
        Staffs = datastaff
    };

    return View(model);
}

I also removed the redundant list creation code since department and staff are already lists.

  • Related