在ASP.net MVC v1.0中显示DataSet内容的最佳实践是什么?

时间:2010-07-12 19:55:36

标签: asp.net asp.net-mvc dataset

我很熟悉在ASP.net表单中使用ASP.net GridView来在网页上显示DataSet的内容。

在ASP.net MVC中显示DataSet的内容的最佳做法是什么?我可以在我的控制器中将DataSet添加到我的DataVew字典中,但我不确定如何在“查看”页面中显示它。

3 个答案:

答案 0 :(得分:1)

您可能想查看我的回答here。如果您仍有疑问,我会很乐意尽我所能。

答案 1 :(得分:0)

DataSets对于ASP.NET MVC应用程序来说并不常见。没有服务器端控件允许您在WebForms中显示DataSet的内容作为GridView控件。因此,如果您正在寻找最佳实践,我建议您实施一些服务器端处理,将您的DataSet转换为.NET对象的层次结构,您将传递给强类型视图。当然,如果这是一个新的应用程序,您甚至不会在DataSet表单中获取数据,但是您将使用直接为您提供.NET对象的ORM。

所以,一旦你拥有了这些物体,你就可以看一下MVCContrib Grid helper。当然,当您使用ASP.NET MVC 1.0时,您必须确保下载针对它编译的MVCContrib的正确版本,因为当前版本是针对ASP.NET MVC 2.0编译的。

答案 2 :(得分:0)

如果您使用的是SQL Server,那么对于MVC,您希望使用Entity Framework。由于Oracle对我的回应是对微软没有给出规格的蹩脚回应(DEVART多年来一直在为实体框架做Oracle)

无论如何,如果你有一个数据集(如果你不需要繁重的数据集就使用数据表)那么为了让你的生活变得简单,你需要将数据集转换为List(如果你从web上提供了一个数据集)服务或层...... 因此像

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

然后在你的控制器中:     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);

然后在你的视图中:

   @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>
    .......
   }