获取有关在asp.net MVC3下拉列表数据根据列表(Get list on basis of d

2019-09-03 03:05发布

我有我的模块中的两个dropdownlists。

在一个下拉列表,我已经硬编码的所有运营商如<,>,<=,>=,==

在第二个下拉列表,我已经硬编码了员工的工资像1000,2000,3000,4000....50000

现在,如果我选择<从一个列表和2000从第二个列表,点击提交按钮,我应该得到谁拥有工资不到2000名员工的名单。

我想这样做在asp.net MVC3

我怎样才能完成这个任务? 我需要为此编写存储过程?

我创建下拉列表,如:

viewModel.OperatorsList = new[]
{
  new SelectListItem { Value = "<", Text = "<" },
  new SelectListItem { Value = ">", Text = ">" },  
  new SelectListItem { Value = "<=", Text = "<=" },
  new SelectListItem { Value = ">=", Text = ">=" },
  new SelectListItem { Value = "==", Text = "==" }
};

viewModel.SalaryList = new[]
{
  new SelectListItem { Value = "1000", Text = "1000" },
  new SelectListItem { Value = "2000", Text = "2000" },  
  new SelectListItem { Value = "3000", Text = "3000" },

  // and so on
};

我已经使用它来显示下拉列表中的观点:

<%: Html.DropDownListFor(x => x.Operators, Model.OperatorsList)%>

Answer 1:

好了,你可以做这样的事情

假设viewModel是...您的viewModel ,你已经有了一个实体Employee与属性Salaryint在此示例中,它可能是一个decimal在现实世界)

创建一个静态辅助类

public static class MyHelper
    {
        // a dictionary for your operators and corresponding ExpressionType
        public static Dictionary<string, ExpressionType> ExpressionTypeDictionary = new Dictionary<string, ExpressionType>
        {
            {"<", ExpressionType.LessThan},
            {">", ExpressionType.GreaterThan},
            {">=", ExpressionType.GreaterThanOrEqual}
            //etc
        };
        //a method to filter your queryable
        public static IQueryable<Employee> FilterSalary(this IQueryable<Employee> queryable, int salary, string operatorType)
        {
            //left part of the expression : m
            var parameter = Expression.Parameter(typeof(Employee), "m");
            //body is the right part of the expression : m
            Expression body = parameter;
            //m.Salary
            body = Expression.Property(body, "Salary");
            //m.Salary <= 1000 (for example)
            body = Expression.MakeBinary(ExpressionTypeDictionary[operatorType], body, Expression.Constant(salary));
            //m => m.Salary <=1000
            var lambda = Expression.Lambda<Func<Employee, bool>>(body, new[] { parameter });
            //so it will be queryable.Where(m => m.Salary <= 1000)
            return queryable.Where(lambda);
        }
}

用法

var queryable = context.All<Employee>();//or something like that, returning an IQueryable<Employee>
queryable = queryable.FilterSalary(viewModel.Salary, viewModel.Operators);


文章来源: Get list on basis of dropdownlist data in asp.net mvc3