2013-05-14 8 views
6

Ho due elenchi a discesa nel mio modulo.Ottieni l'elenco sulla base dei dati dell'elenco a discesa in asp.net mvc3

In una DropDownList, ho hardcoded tutti gli operatori come <,>,<=,>=,==

Al secondo DropDownList, ho hardcoded stipendio dei dipendenti come 1000,2000,3000,4000....50000

Ora, se seleziono < da una lista e 2000 dalla seconda lista e clic sul pulsante di invio dovrei ottenere l'elenco dei dipendenti che hanno lo stipendio meno di 2000.

io voglio fare questo in asp.net MVC3

Come posso eseguire questa operazione ???

Devo scrivere la stored procedure per questo?

qualcuno può aiutarmi ??

ho creato DropDownList come:

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" }, 
           . 
           . 
            }; 

e ho usato questo per mostrare DropDownList in vista:

<%: Html.DropDownListFor(x => x.Operators, Model.OperatorsList)%> 
+0

si prega di mostrare il codice HTML generato per i vostri due DropDownList. –

+1

e, a proposito, stai usando EF, immagino? –

+0

Questa è la tua domanda? Un si sta usando il framework di entità –

risposta

6

bene, si potrebbe fare qualcosa di simile

assumendo viewModel è ... il tuo viewModel, e tu hai un'entità Employee con una proprietà Salary (int in questo esempio, è probabilmente un decimal nel mondo reale)

creare una classe di supporto statica

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

utilizzo

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

Althaus: Grazie per la tua risposta. Ho capito il dizionario per gli operatori ma ho confuso la classe statica FilterSalry che hai suggerito. puoi per favore spiegarmi qualcosa su questa lezione? –

+0

@ K2_Ketu beh, il metodo di estensione 'Where' richiede un' Expression > 'come parametro. Il metodo è solo costruire questa espressione, trasformarla in una lambda e passarla al metodo Where. Potresti provare a eseguire il debug e vedere cosa succede in 'body' passo dopo passo, e/o google per gli alberi di espressione. Aggiungerò anche qualche commento nel mio codice. –

+0

@ K2_Ketu e FilterSalary non è una classe, solo un metodo di estensione statica, come ... 'Where' –

Problemi correlati