【问题标题】:RuntimeBinderException: The best overloaded method match for 'System.Type.GetProperty(string)' has some invalid argumentsRuntimeBinderException:“System.Type.GetProperty(string)”的最佳重载方法匹配有一些无效参数
【发布时间】:2022-01-08 16:10:14
【问题描述】:

我正在学习 Asp.net Core 并使用 CRUD 操作、SQL 服务器和使用实体框架构建一个简单的 Web。

当应用程序尝试编译时,我会出现此错误:

RuntimeBinderException: The best overloaded method match for 'System.Type.GetProperty(string)' has some invalid arguments 

我知道问题出在这一行,如何在 GetProperty 中传递动态或有其他方法? :

var propertyInfo = typeof(Employee).GetProperty(ViewBag.SortField);

那是我的模特:

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

那是我的控制器:

namespace EmployeesApp.Controllers

{

    public enum SortDirection
    {
        Ascending,
        Descending
    }


    public class EmployeeController : 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;
        }
    }
}   

【问题讨论】:

  • 尝试将其转换为字符串 ((string) ViewBag.SortField)。如果它不是字符串类型,它可能会抛出错误,但这是使用动态类型时必须付出的代价。

标签: c# asp.net asp.net-mvc asp.net-core asp.net-web-api


【解决方案1】:

你有一个错误

ViewBag.SortField = "EmployeeNumber";
ViewBag.SortField = SortDirection.Ascending;

我觉得应该是的

ViewBag.SortField = "EmployeeNumber";
 ViewBag.SortDirection = SortDirection.Ascending;

【讨论】:

  • 已解决,谢谢帮助
  • @ha33 不客气!请不要忘记接受答案,因为它很有帮助。您可以通过单击我的答案站点上的复选标记来完成。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-10
  • 2015-10-14
  • 2018-07-14
  • 1970-01-01
  • 2013-01-07
  • 2014-05-17
相关资源
最近更新 更多