【问题标题】:How to pass ADO.NET DataSet to a view如何将 ADO.NET 数据集传递给视图
【发布时间】:2012-03-15 19:50:19
【问题描述】:

我有一个遗留项目,我们正在慢慢迁移到 MVC,但是有数百个 ADO.NET SQL 数据集对象

我想将数据集保留在模型中并在视图中访问它。这是可能的,还是有更好的方法来做到这一点?创建列表很困难,因为数据太多。我们最终会将 SQL 转换为实体,但不是现在。

例子:

型号:

 string query = "SELECT * FROM dbo.manf";
 SqlDataAdapter adapter = new SqlDataAdapter(query, conn);
 DataSet data = new DataSet();
 adapter.Fill(data, "manf");

查看:

@foreach (DataRow data in manf.Tables["manf"].Rows)
{
    @:manf["id"] + " " + manf["name"]);
}  

【问题讨论】:

  • 你有域对象吗?我倾向于将 DataTable 转换为控制器中的域对象,然后将它们传递给视图。
  • 这不是最好的方法,但是由于您正在迁移旧应用程序...您可以将数据集传递给视图,就好像它是模型实体一样。因此,您的模型将是一个数据集。
  • 对对象使用 Get 和 Sets?嗯。我有一些有 200 列的查询。我认为手写会很耗时。还有什么吗?

标签: c# sql-server asp.net-mvc database ado.net


【解决方案1】:

您可以将所需的任何对象传递给您的视图,因此您的控制器代码应如下所示:

public ViewResult Index() 
{ 
    .... 
    return View(data); 
} 

还有你的观点:

@model System.Data.DataSet

@foreach (DataRow row in Model.Tables["manf"].Rows)    
{    
    @(row["id"] + " " + row["name"])
}  

传递的对象将存储在视图通用基类的 Model 属性中,您可以使用 Model 访问它。 @model 定义了传递的对象的数据类型。

无论如何,任何代码都将被 HTML 编码,不需要 :。如果您不想编码,则必须使用@Html.Raw(...)。

编辑:您只能将一个参数作为模型移动。如果您想使用多个对象,您可以定义一个新的数据类型(一个 ViewModel),它包含这两个对象的属性。对于窗口标题等简单对象,您可以使用 ViewBag。

ViewBag.Title = myTitle;

ViewBag 是控制器的动态属性,基本上是 ViewData 的包装器。

【讨论】:

  • 完美。我可以为 view() 使用超过 1 个参数吗?我正在使用一个选择视图的 case 语句,所以我需要: view("pagename",data) ?
  • 我似乎无法将数据作为数据集传递。有什么我想念的吗?
  • 有错误信息吗?您不能使用视图(“页面名称”,数据)。关于多个参数:我在答案中添加了更多信息,可以使用 ViewBag 或包含这两个属性的新数据类型。
【解决方案2】:

控制器代码

     //pQ is your query you have created 
     //P4DAL is the key name for connection string 

       DataSet ds = pQ.Execute(System.Configuration.ConfigurationManager.ConnectionStrings["Platform4"].ConnectionString);


      //ds will be used below
      //create your own view model according to what you want in your view 
     //VMData is my view model

     var _buildList = new List<VMData>();
                {
                    foreach (DataRow _row in ds.Tables[0].Rows)
                    {
                        _buildList.Add(new VMData
                        {  
         //chose what you want from the dataset results and assign it your view model fields 

                            clientID = Convert.ToInt16(_row[1]),
                            ClientName = _row[3].ToString(),
                            clientPhone = _row[4].ToString(),
                            bcName = _row[8].ToString(),
                            cityName = _row[5].ToString(),
                            provName = _row[6].ToString(),
                        });



                    }

                }

      //you will use this in your view
      ViewData["MyData"] = _buildList;

查看

    @if (ViewData["MyData"] != null)
    {

        var data = (List<VMData>)ViewData["MyData"];
        <div class="table-responsive">
            <table class="display table"  id="Results">

                <thead>
                    <tr>
                        <td>Name</td>
                        <td>Telephone</td>
                        <td>Category </td>
                        <td>City </td>
                        <td>Province </td>

                    </tr>
                </thead>

                <tbody>
                    @foreach (var item in data)
                    {
                <tr>
                    <td>@Html.ActionLink(item.ClientName, "_Display", new { id = item.clientID }, new { target = "_blank" })</td>
                    <td>@item.clientPhone</td>
                    <td>@item.bcName</td>
                    <td>@item.cityName</td>
                    <td>@item.provName</td>

                </tr>

                }
                </tbody>


            </table>
            </div>
            }

【讨论】:

    猜你喜欢
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 2011-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多