DataTable instead of Entity framework in Asp.Net MVC

前端 未结 2 996
无人及你
无人及你 2021-01-03 12:49

How do I use Datatable instead of Entity framework in Asp.Net mvc?

I\'m referring to this tutorial on the asp.net Website .... http://www.asp.net/Learn/mvc/tutorial-

相关标签:
2条回答
  • 2021-01-03 13:31

    If I understand your question correctly, just because you are using MVC you do not have to use Entity framework to communicate with the database.

    Inside your controller you can use whatever means you want to get the data out of the database. Then you can either transform that data into a custom object which you pass to your View, or you could potentially just pass a DataTable to your view.

    Your view just has to know how to iterate through your object that you pass to it.

    However, I recommend using POCO objects to pass to your view, so that you don't tightly couple your view data to the technology used to extract information from the database.

    0 讨论(0)
  • 2021-01-03 13:52

    You can return whatever you want provided it can be stored (serialized) in the ViewData. There is nothing "magic" about ASP.NET MVC that constrains values and/or types.

    If you want to iterate over the DataTable in the view, put it in ViewData in the controller, retrieve it in the View, and iterate over it as you would anywhere else.

    DataTable is serializable.

    So something similar to the following should work:

    <%
    var tbl = ViewData["MyDataTable"];
    
    foreach (DataRow row in tbl.Rows)
    {
      foreach (DataColumn col in tbl.Columns)
      {
        Response.Write(row[col] as string ?? string.Empty);
      }
    }
    %>
    
    0 讨论(0)
提交回复
热议问题