根据asp.net mvc3中的下拉列表数据获取列表 [英] Get list on basis of dropdownlist data in asp.net mvc3
问题描述
我的模块中有两个下拉列表.
I have two dropdownlists in my module.
在一个下拉列表中,我对所有操作符进行了硬编码,例如 <,>,<=,>=,==
In one dropdownlist, I have hardcoded all the operators like <,>,<=,>=,==
在第二个下拉列表中,我对员工的工资进行了硬编码,例如 1000,2000,3000,4000....50000
In second dropdownlist, I have hardcoded salary of employees like 1000,2000,3000,4000....50000
现在,如果我从一个列表中选择 <
并从第二个列表中选择 2000
并单击提交按钮,我应该会得到工资低于 2000 的员工列表.
Now if I select <
from one list and 2000
from second list and click on submit button I should get list of employees who have salary less than 2000.
我想在 asp.net mvc3 中做到这一点
I want to do this in asp.net mvc3
我怎样才能完成这个任务?我需要为此编写存储过程吗?
How can I accomplish this task? Do I need to write a stored procedure for this?
我创建了如下下拉列表:
I have created dropdownlist like:
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
};
我用它来在视图中显示下拉列表:
and I have used this to show dropdownlist in view:
<%: Html.DropDownListFor(x => x.Operators, Model.OperatorsList)%>
推荐答案
好吧,你可以这样做
假设 viewModel
是...你的 viewModel
,并且你有一个实体 Employee
和属性 Salary代码>(本示例中的
int
,在现实世界中可能是decimal
)
assuming viewModel
is... your viewModel
, and you've got an entity Employee
with a property Salary
(int
in this sample, it's probably a decimal
in real world)
创建一个静态辅助类
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);
这篇关于根据asp.net mvc3中的下拉列表数据获取列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!