Get list based on dropdownlist data in asp.net mvc3

I have two dropdownlists in my module.

In one drop-down list, I hard-coded all operators like <,>,<=,>=,==

In the second drop-down list, I have a salary with a hard pointer, for example 1000,2000,3000,4000....50000

Now, if I select < from one list and 2000 from the second list and press the submit button, I should get a list of employees whose salaries are less than 2000.

I want to do this in asp.net mvc3

How can I complete this task ???

Do I need to write a stored procedure for this?

can someone help me?

I created a dropdownlist as:

  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 used this to show a dropdown:

  <%: Html.DropDownListFor(x => x.Operators, Model.OperatorsList)%> 
+6
source share
1 answer

Ok you could do something like this

Assuming viewModel is ... your viewModel , and you have an Employee object with the Salary property ( int in this example, this is probably decimal in the real world)

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

Using

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

Source: https://habr.com/ru/post/944972/


All Articles