controller - Get list on basis of dropdownlist data in asp.net mvc3 -
i have 2 dropdownlists in module.
in 1 dropdownlist, have hardcoded operators <,>,<=,>=,==
in second dropdownlist, have hardcoded salary of employees 1000,2000,3000,4000....50000
now if select <
1 list , 2000
second list , click on submit button should list of employees have salary less 2000.
i want in asp.net mvc3
how can accomplish task???
do need write stored procedure this?
can 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 have used show dropdownlist in view:
<%: html.dropdownlistfor(x => x.operators, model.operatorslist)%>
well, that
assuming viewmodel
is... viewmodel
, , you've got entity employee
property salary
(int
in sample, it's decimal
in real world)
create static helper class
public static class myhelper { // dictionary operators , corresponding expressiontype public static dictionary<string, expressiontype> expressiontypedictionary = new dictionary<string, expressiontype> { {"<", expressiontype.lessthan}, {">", expressiontype.greaterthan}, {">=", expressiontype.greaterthanorequal} //etc }; //a method filter queryable public static iqueryable<employee> filtersalary(this iqueryable<employee> queryable, int salary, string operatortype) { //left part of expression : m var parameter = expression.parameter(typeof(employee), "m"); //body right part of 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 queryable.where(m => m.salary <= 1000) return queryable.where(lambda); } }
usage
var queryable = context.all<employee>();//or that, returning iqueryable<employee> queryable = queryable.filtersalary(viewmodel.salary, viewmodel.operators);
Comments
Post a Comment