MVC数据集与viewbags [英] MVC datasets with viewbags

查看:156
本文介绍了MVC数据集与viewbags的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何把数据集中到一个viewbag并将结果显示在视图中?

我有我写一个viewbag模型的数据集。我想用一个foreach循环来获取数据行出在视图中viewbag的。

我已经有一个变量进入的观点,所以我不能正常通过数据集。我也将有每页许多其他数据集。所以我想viewbag是解决这个问题的最佳途径。

模式

 类modeldata
{
    公共数据集readrows(DataSet的数据集)
    {
    //从SQL查询返回的数据。
    }
}

控制器:

 数据集的数据=新的DataSet();
 modeldata的getData =新modeldata();
 ViewBag.Data = getdata.readrows(数据);
 返回查看基于case语句(第1页)//。
 //已经有值进入视图,因此我需要使用viewbag

查看:

  @Model site.controllers.homecontroller;     的foreach(在ViewBag.Data.Rows Model.data行)
        {
            @:行[ID] ++行[名称];
        }


解决方案

要在视图中显示的数据,你有两个选择。一个是模型类的一个实例传递到强类型的视图。第二个选择是使用ViewBag。对你来说,它看起来像你正在做的都一点点,但我会建议使用强类型的视图方式。

该视图将有一个重新presents你的 @Model 模式属性$ C>声明。在您的code,您使用的是行不通的控制器类。我改写的例子使用的DataSet 为模型。正如你所看到的,视图的模式属性变为 System.Data.DataSet中类的一个实例,具有它的所有属性和方法。

查看

  @Model System.Data.DataSet中;的foreach(在Model.Rows的DataRow行)
{
    @:行[ID] ++行[名称];
}

控制器

 数据集的数据=新的DataSet();
modeldata的getData =新modeldata();
返回查看(getdata.readrows(数据));

编辑:

下面是使用字典中的模型类存储多个数据集的例子。然后,您可以修改视图使用modeldata类型作为其Model类。

型号

 命名空间Site.Models
{
    类modeldata
    {
        公共字典<字符串,数据集>数据集{搞定;组; }        公共静态数据集ReadRows(DataSet的数据集)
        {
            //从SQL查询返回的数据。
        }
    }
}

查看

  @Model Site.Models.modeldata;@foreach(System.Data.DataTable表Model.DataSets [的sampleData]。表)
{
    的foreach(在table.Rows的System.Data.DataRow行)
    {
        @:行[ID] ++行[名称];
    }
}

控制器

 数据集的数据=新的DataSet();
modeldata的getData =新modeldata();
getdata.DataSets [的sampleData] = modeldata.ReadRows(数据);
返回查看(的getData);

How do I put a dataset into a viewbag and display the result in a view?

I have a dataset from a model that I write to a viewbag. I want to use a foreach loop to get the datarows out of the viewbag in the view.

I already have a variable going into the view, so I cant pass the dataset normally. I will also have many other datasets per page. so I thought viewbag was the best way to approach this issue.

model

class modeldata 
{
    public dataset readrows(DataSet dataset)
    {
    //returns data from sql query.
    }
}

controller:

 DataSet data = new DataSet();
 modeldata getdata = new modeldata ();
 ViewBag.Data = getdata.readrows(data);
 return view("page1") //based on case statement. 
 //Already have a value going into view, so I need to use viewbag

View:

@Model site.controllers.homecontroller;

     foreach (Model.data row in ViewBag.Data.Rows)
        {
            @:row["id"] + " " + row["name"];
        } 

解决方案

To display data in the view, you have two options. One is to pass an instance of a Model class to a strongly-typed view. The second option is to use ViewBag. In your case, it looks like you are doing a little bit of both, but I would recommend using the strongly-typed view approach.

The View will have a Model property that represents an instance of the class type specified by your @Model declaration. In your code, you are using the controller class which is not going to work. I rewrote the example to use a DataSet as the model. As you can see, the Model property of the View becomes an instance of the System.Data.DataSet class and has all of its properties and methods.

View

@Model System.Data.DataSet;

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

Controller

DataSet data = new DataSet();
modeldata getdata = new modeldata();
return View(getdata.readrows(data));

Edit:

Here is an example that uses a Dictionary in the model class to store multiple DataSets. You can then modify the View to use the modeldata type as its Model class.

Model

namespace Site.Models
{
    class modeldata
    {
        public Dictionary<string, DataSet> DataSets { get; set; }

        public static DataSet ReadRows(DataSet dataset)
        {
            //returns data from sql query.
        }
    }
}

View

@Model Site.Models.modeldata;

@foreach (System.Data.DataTable table in Model.DataSets["sampleData"].Tables)
{
    foreach (System.Data.DataRow row in table.Rows)
    {
        @:row["id"] + " " + row["name"];
    }
} 

Controller

DataSet data = new DataSet();
modeldata getdata = new modeldata();
getdata.DataSets["sampleData"] = modeldata.ReadRows(data);
return View(getdata);

这篇关于MVC数据集与viewbags的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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