Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 <,>,<=,>=,==

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 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)%>
like image 484
Keren Caelen Avatar asked May 14 '13 12:05

Keren Caelen


1 Answers

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);
like image 107
Raphaël Althaus Avatar answered Sep 28 '22 16:09

Raphaël Althaus