如何通过ADO.NET数据集到一个视图 [英] How to pass ADO.NET DataSet to a view

查看:293
本文介绍了如何通过ADO.NET数据集到一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个传统项目,我们正在慢慢移动到MVC,但也有数以百计的ADO.NET数据集的SQL对象的

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

例如:

型号:

 查询字符串=SELECT * FROM dbo.manf;
 SqlDataAdapter的适配器=新的SqlDataAdapter(查询,康涅狄格州);
 数据集的数据=新的DataSet();
 adapter.Fill(数据,MANF);
 

查看:

  @foreach(在manf.Tables DataRow的数据[MANF。行)
{
    @:MANF [ID] ++ MANF [名称]);
}
 

解决方案

您可以通过您希望在查看任何对象,所以你的控制器code应该是这样的:

 公开的ViewResult指数()
{
    ....
    返回查看(数据);
}
 

和你的观点:

  @model System.Data.DataSet中

@foreach(DataRow的行Model.Tables [MANF。行)
{
    @(行[ID] ++行[名称])
}
 

传递的对象将被存储在所述视图的通用基类的模型属性并且可以与模型访问它。所述@model定义传递的对象的数据类型。

任何code将HTML连接codeD反正:不需要。如果你不想EN code,你将不得不使用@ Html.Raw(...)。

编辑:你只能移动一个参数的一种模式。如果你想使用一个以上的对象,你要么定义一个新的数据类型(一个视图模型),它containes性能,非常适合。为简单物体,例如一个窗口标题可以使用ViewBag

  ViewBag.Title = myTitle;
 

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

I have a legacy project that we are slowly moving to MVC, but there are hundreds of ADO.NET SQL DataSet objects

I would like to keep the data sets in the model and access it in the view. Is this possible, or is there a better way to do this? Creating lists is difficult because there is too much data. We will eventually convert the SQL to entity, but not now.

Example:

Model:

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

View:

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

解决方案

You can pass any object you want to your View, so your controller code should look like this:

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

And your View:

@model System.Data.DataSet

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

The passed object will be stored in the Model property of the generic base class of the view and you can access it with Model. The @model defines the data type of the object passed.

Any code will be HTML encoded anyway, the : is not needed. If you don't want to encode, you will have to use @Html.Raw(...).

EDIT: you can only move one parameter as a model. If you want to use more than one object, you either define a new datatype (a ViewModel) which containes properties for both. For simple objects such as a window title you can use the ViewBag.

ViewBag.Title = myTitle;

The ViewBag is a dynamic property of the controller and basically a wrapper over ViewData.

这篇关于如何通过ADO.NET数据集到一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆