ribha shakoor

ribha shakoor

  • NA
  • 76
  • 2.5k

Get controller action linq query for complex binding model

Nov 9 2019 12:32 PM

I need to convert static query of complex model binding to the dynamic LINQ query in controller action to get data from database through EF

This is my Model
  1. public class DepartmentViewModel  
  2. {  
  3.   public int DepartmentId { get; set; }  
  4.   public string Name {get; set;}  
  5.   public List stdList { get; set; }  
  6.   
  7.  public DepartmentViewModel()  
  8.   {  
  9.     stdList = new List();  
  10.   }  
  11. }  
  12.   
  13. public class StdListModel  
  14. {  
  15.   public int ID { get; set; }  
  16.   public string Name { get; set; }  
  17. }  
and controller is as:
  1. public ActionResult Index()  
  2.     {  
  3.         List list = new List();  
  4.         DepartmentViewModel dept = new DepartmentViewModel();  
  5.   
  6.         dept.DepartmentId = 1;  
  7.         dept.Name = "Department 1";  
  8.         dept.stdList.Add(new StdListModel() { ID = 1, Name = "Some Name" });  
  9.         dept.stdList.Add(new StdListModel() { ID = 2, Name = "Another Name" });  
  10.         list.Add(dept);  
  11.   
  12.         return View(list);  
  13.     }  
i need to change this controller action static list to dynamic linq query to get data from database EF and bind to Model.

Answers (3)