开发者

What is the best practice for displaying the contents of a DataSet in ASP.net MVC v1.0?

开发者 https://www.devze.com 2023-01-07 12:43 出处:网络
I am long familiar with using the ASP.net GridView in ASP.net forms to display the contents of DataSet on a web page.

I am long familiar with using the ASP.net GridView in ASP.net forms to display the contents of DataSet on a web page.

What is the best practice for displaying the contents of the DataSet in ASP.net MVC? I can add the DataSet to my DataVew dictionary in my controller, but I'm unsure of how to display it in the View page开发者_开发知识库.


You may want to view my answer here. If you still have questions I'll be glad to do what I can.


DataSets are uncommon for ASP.NET MVC applications. There are no server side controls that will allow you to display the contents of a DataSet as the GridView control in WebForms. So if you are looking for a best practice, I would suggest you put in place some server side processing that will convert your DataSet into an hierarchy of .NET objects that you would pass to a strongly typed view. Of course if this is a new application you wouldn't even bother fetching the data in a DataSet form but you would use an ORM that will directly give you .NET objects.

So once you have the objects you could take a look at the MVCContrib Grid helper. Of course as you are using ASP.NET MVC 1.0 you will have to make sure to download the proper version of MVCContrib which is compiled against it because the current version is compiled against ASP.NET MVC 2.0.


If you are using SQL Server, then for MVC you want to be using Entity Framework. Since Oracle responded to me with a lame response about Microsoft not giving them specs ( DEVART has been doing Oracle to Entity Framework for years )

Regardless, if you have a dataset ( use a datatable if you do not need a heavy dataset ) then to make your life easy you will want to convert your dataset to a List (if you are supplied a dataset from say a web service or layer ... Thus something like

    public IEnumerable<IClient> GetClient(IClient client)
    {
       DataSet dataSet = ....
         .....
      List<IClient> clients = (from c in dataSet.Tables[0].AsEnumerable()
                                 select new Client()
       .....
       return clients;
    }

Then in your controller: IClient client = (IClient)TempData["Client"];

        // Instantiate and instance of the repository 
        var repository = new Get_Client_Repository();
        // Set a model object to return the dynamic list from repository method call passing in the parameter data
        var model = repository.GetClient(client);

        // Call the View up passing in the data from the list
        return View(model);

Then in your View:

   @model IEnumerable<CISOnlineMVC.DAL.IClient>

   @foreach (var item in Model) {

     <tr>
    <td>
        @Html.ActionLink("Select", "ClientDetails", "Cis", new { id = item.ClientId }, null) |
    </td>
    <td>
        @item.LastName
    </td>
    .......
   }
0

精彩评论

暂无评论...
验证码 换一张
取 消