ribha shakoor

ribha shakoor

  • NA
  • 76
  • 2.3k

Get controller action linq query for complex binding model

Nov 9 2019 1:40 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. public DepartmentViewModel()
  7. {
  8. stdList = new List();
  9. }
  10. }
  11. public class StdListModel
  12. {
  13. public int ID { get; set; }
  14. public string Name { get; set; }
  15. }
and controller is as:
  1. public ActionResult Index()
  2. {
  3. List list = new List();
  4. DepartmentViewModel dept = new DepartmentViewModel();
  5. dept.DepartmentId = 1;
  6. dept.Name = "Department 1";
  7. dept.stdList.Add(new StdListModel() { ID = 1, Name = "Some Name" });
  8. dept.stdList.Add(new StdListModel() { ID = 2, Name = "Another Name" });
  9. list.Add(dept);
  10. return View(list);
  11. }
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)