Home > other >  Argument 1: cannot convert from 'System.Collections.Generic.List' to 'System.Collecti
Argument 1: cannot convert from 'System.Collections.Generic.List' to 'System.Collecti

Time:12-01

I'm learning Asp.net Core and building a simple web with CRUD operations, SQL server and using Entity Framework. When I try to build a sorting method I git this error in this line at parameter employees

return View(this.SortEmployees(employees, SortField, CurrentSortField, SortDirection));

and that's the error:

Severity    Code    Description Project File    Line    Suppression State
Error   CS1503  Argument 1: cannot convert from 'System.Collections.Generic.List<EmployeesApp.Models.Employee>' to 'System.Collections.Generic.List<EmployeesApp.Controllers.Employee>' EmployeesApp    

that's my Model:

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace EmployeesApp.Models
{
    [Table("Employee", Schema ="dbo")]

    public class Employee
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Display(Name ="Employee ID")]
        public int EmployeeId { get; set; }


        [Required]
        [Column(TypeName ="varchar(5)")]
        [MaxLength(5)]
        [Display(Name ="Employee Number")]
        public string EmployeeNumber { get; set; }


        [Required]
        [Column(TypeName = "varchar(150)")]
        [MaxLength(100)]
        [Display(Name = "Employee Name")]
        public string EmployeeName { get; set; }


        [Required]
        [DataType(DataType.Date)]
        [Display(Name ="Date of Birth")]
        [DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}")]
        public DateTime DOB { get; set; }


        [Required]
        [DataType(DataType.Date)]
        [Display(Name = "Hiring Date")]
        [DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}")]
        public DateTime HiringDate { get; set; }


        [Required]
        [Column(TypeName ="decimal(12,2)")]
        [Display(Name ="Gross Salary")]
        public decimal GrossSalary { get; set; }


        [Required]
        [Column(TypeName = "decimal(12,2)")]
        [Display(Name = "Net Salary")]
        public decimal NetSalary { get; set; }


        [ForeignKey("Department")]
        [Required]
        public int DepartmentId { get; set; }



        [Display(Name = "Department")]
        [NotMapped]
        public string DepartmentName { get; set; }


        public virtual Department Department { get; set; }
    }
}

and that's my COntroller:

namespace EmployeesApp.Controllers

{

    public enum SortDirection
    {
        Ascending,
        Descending
    }


    public class Employee : Controller
    {
       
        HRDatabaseContext dbContext = new HRDatabaseContext();

        public IActionResult Index(string SortField, string CurrentSortField, SortDirection SortDirection)
        {
            var employees = GetEmployees();
            return View(this.SortEmployees(employees, SortField, CurrentSortField, SortDirection));
        }



        private List<Models.Employee> GetEmployees()
        {
            return (from Employee in dbContext.Employees
                    join Department in dbContext.Departments on Employee.DepartmentId equals Department.DepartmentId
                    select new Models.Employee
                    {
                        EmployeeId = Employee.EmployeeId,
                        EmployeeName = Employee.EmployeeName,
                        DOB = Employee.DOB,
                        HiringDate = Employee.HiringDate,
                        GrossSalary = Employee.GrossSalary,
                        NetSalary = Employee.NetSalary,
                        DepartmentId = Employee.DepartmentId,
                        DepartmentName = Department.DepartmentName

                    }).ToList();
        }


        public IActionResult Add()
        {
            ViewBag.Department = this.dbContext.Departments.ToList();
            return View();
        }


        [HttpPost]
        public IActionResult Add(Models.Employee model)
        {
            ModelState.Remove("EmployeeID");
            ModelState.Remove("Department");
            ModelState.Remove("DepartmentName");
            if (ModelState.IsValid)
            {
                dbContext.Employees.Add(model);
                dbContext.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.Department = dbContext.Departments.ToList();
            return View("Add", model);
        }


        public IActionResult Edit(int ID)
        {
            HRDatabaseContext dbContext1 = dbContext;
            Models.Employee data = dbContext1.Employees.Where(e => e.EmployeeId == ID).FirstOrDefault();
           

            ViewBag.Department = this.dbContext.Departments.ToList();
            return View("Add", data);
        }


        [HttpPost]
        public IActionResult Edit(Models.Employee model)
        {
            ModelState.Remove("EmployeeID");
            ModelState.Remove("Department");
            ModelState.Remove("DepartmentName");
            if (ModelState.IsValid)
            {
                dbContext.Employees.Update(model);
                dbContext.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.Department = dbContext.Departments.ToList();
            return View();
        }


        public IActionResult Delete(int ID)
        {
            Models.Employee data = this.dbContext.Employees.Where(e => e.EmployeeId == ID).FirstOrDefault();

            if (data != null)
            {
                dbContext.Employees.Remove(data);
                dbContext.SaveChanges();
            }

            return RedirectToAction("Index");
        }

     
        private List<Employee> SortEmployees(List<Employee> employees, String sortField, string currentSortField, SortDirection sortDirection)
        
        {
            if (string.IsNullOrEmpty(sortField))
            {
                
                ViewBag.SortField = "EmployeeNumber";
                ViewBag.SortField = SortDirection.Ascending;

            }
            else
            {
                if (currentSortField == sortField)
                
                {
                    ViewBag.SortDirection = sortDirection == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending;
                    
                }
                else
                    ViewBag.SortDirection = sortDirection == SortDirection.Ascending;
                    ViewBag.SortField = sortField;

            }

            //* create the sorting proccess
            var propertyInfo = typeof(Employee).GetProperty(ViewBag.SortField);
            if (ViewBag.SortDirection == SortDirection.Ascending)
            {
                employees = employees.OrderBy(e => propertyInfo.GetValue(e, null)).ToList();
            }
            else
            {
                employees = employees.OrderByDescending(e => propertyInfo.GetValue(e, null)).ToList();

            }

            return employees;
        }
    }
}   

CodePudding user response:

Your SortEmployees method in your controller takes a List<Employee> which in this context is a Controllers.Employee and not a Models.Employee as you suspect.

The best solution in this case is to rename your Controller to EmployeeController to follow the ASP.NET convention. In ASP.NET controllers are always named [Name]Controller.

CodePudding user response:

    private List<Employee> SortEmployees(List<Employee> employees, String sortField, string currentSortField, SortDirection sortDirection)
    
    {

The issue relates the above SortEmployees method, its parameter and return data using the EmployeesApp.Controllers.Employee model, istead of the EmployeesApp.Models.Employee. So, when calling this method, it will show this error.

To solve this issue, try to modify the SortEmployees method as below: using the Models.Employee

    private List<Models.Employee> SortEmployees(List<Models.Employee> employees, String sortField, string currentSortField, SortDirection sortDirection)
    {
  • Related