获取在asp.net MVC3的DropDownList数据的基础上名单 [英] Get list on basis of dropdownlist data in asp.net mvc3
问题描述
我有两个dropdownlists我的模块中。
在一个DropDownList中,我已经很难codeD中的所有像&LT运营商;,>,< =,> =,==
在第二个DropDownList的,我的员工像辛勤codeD工资 1000,2000,3000,4000 .... 50000
现在,如果我选择<
从一个列表和 2000
从第二个列表,点击提交按钮我应该谁拿到的薪水有不到2000名员工的名单。
我想这样做在asp.net MVC3
我怎样才能完成这个任务?
我是否需要写存储过程呢?
谁能帮助我?
我创建的DropDownList这样的:
viewModel.OperatorsList =新[]
{
新SelectListItem {值=<文本=< },
新SelectListItem {值=>中,文本=>中},
新SelectListItem {值=< =文本=< =},
新SelectListItem {值=> =文本=> =},
新SelectListItem {值===,文本===}
};
viewModel.SalaryList =新[]
{
新SelectListItem {值=1000,文字=1000},
新SelectListItem {值=2000,文字=2000},
新SelectListItem {值=3000,文字=3000},
。
。
};
和我用它来显示视图中的DropDownList:
<%:Html.DropDownListFor(X => x.Operators,Model.OperatorsList)%GT;
好了,你可以做这样的事情。
假设视图模型
...是你的视图模型
,和你有一个实体员工
与属性薪酬
( INT
在此示例中,它可能是一个小数
在现实世界中)
创建一个静态辅助类
公共静态类MyHelper
{
//一个字典操作员以及相应的防爆pressionType
公共静态字典<字符串,防爆pressionType>防爆pressionTypeDictionary =新词典<字符串,防爆pressionType>
{
{<防爆pressionType.LessThan},
{>中,前pressionType.GreaterThan},
{> =,前pressionType.GreaterThanOrEqual}
//等等
};
//过滤您可查询的方法
公共静态的IQueryable<员工> FilterSalary(这IQueryable的<员工>可查询,INT工资,串operatorType)
{
恩pression的//左半部分:M
变量参数=前pression.Parameter(typeof运算(员工),M);
//身体是前pression的右侧部分:M
防爆pression体=参数;
//m.Salary
机身前= pression.Property(身体,工资);
//m.Salary&所述; = 1000(例如)
机身前= pression.MakeBinary(前pressionTypeDictionary [operatorType],车身,前$ P $ pssion.Constant(工资));
// M => m.Salary< = 1000
VAR波长=前pression.Lambda<&Func键LT;员工,布尔>>(身体,新的[] {参数});
//所以这将是queryable.Where(M => m.Salary&下; = 1000)
返回queryable.Where(拉姆达);
}
}
使用
VAR可查询= context.All<员工>(); //或类似的东西,并返回一个IQueryable<员工>
可查询= queryable.FilterSalary(viewModel.Salary,viewModel.Operators);
I have two dropdownlists in my module.
In one dropdownlist, I have hardcoded all the operators like <,>,<=,>=,==
In second dropdownlist, I have hardcoded salary of employees like 1000,2000,3000,4000....50000
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.
I want to do this in asp.net mvc3
How can I accomplish this task???
Do I need to write stored procedure for this?
can anyone help me??
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 I have used this to show dropdownlist in view:
<%: Html.DropDownListFor(x => x.Operators, Model.OperatorsList)%>
well, you could do something like that
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)
create a static helper class
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);
}
}
usage
var queryable = context.All<Employee>();//or something like that, returning an IQueryable<Employee>
queryable = queryable.FilterSalary(viewModel.Salary, viewModel.Operators);
这篇关于获取在asp.net MVC3的DropDownList数据的基础上名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!