Sneha Dhandapani

Sneha Dhandapani

  • 1.4k
  • 471
  • 136.3k

How to bind dynamic table in Datatable in mvc?

Sep 18 2020 11:43 PM
Hi i want to bind my table with datatable dynamically. I wrote upto DAL and Controller. But I am totally struck how to bind this table in view . I tried web grid but requirement is not web grid i want to bind the table column and row dynamically in datatable. Anyone help me to resolve this issue.Thanks
 
My DAL
  1. public List<dynamic> GetData(string Year, string SID, string Type_ID)  
  2. {  
  3. ISqlDBHelper sqlDBHelper = new SqlDBHelper();  
  4. SqlParameter[] parameters = new SqlParameter[]{  
  5. new SqlParameter("@Year",Year),  
  6. new SqlParameter("@ConID",SID),  
  7. new SqlParameter("@TypeID",Type_ID),  
  8. };  
  9. using (DataTable table = sqlDBHelper.ExecuteParamerizedSelectCommand("GetDetails", CommandType.StoredProcedure, parameters))  
  10. {  
  11. var model = new List<dynamic>(table.Rows.Count);  
  12. if (table.Rows.Count > 0)  
  13. {  
  14. foreach (DataRow row in table.Rows)  
  15. {  
  16. var obj = (IDictionary<stringobject>)new ExpandoObject();  
  17. foreach (DataColumn col in table.Columns)  
  18. {  
  19. obj.Add(col.ColumnName, row[col.ColumnName]);  
  20. }  
  21. model.Add(obj);  
  22. }  
  23. }  
  24. return model;  
  25. }  
  26. }  
My Controller
  1. public PartialViewResult GetDetails(string Year = "3"string SID = "1"string TypeID = "1")  
  2. {  
  3. try  
  4. {  
  5. SBAL sBal = new SBAL();  
  6. return PartialView(sBal .GetData(Year, SID , TypeID ));  
  7. }  
  8. catch (Exception ex)  
  9. {  
  10. Common.AddModel_Log4Net();  
  11. throw new Exception(ex.ToString());  
  12. }  
  13. }  

Answers (2)