我如何 return 来自控制器的多个数据集?
How can I return multiple datasets from a Controller?
Return从控制器获取单个 DataTable 对我来说工作正常:
public ActionResult Index()
{
DataTable dtable = new DataTable();
dtable = SQL.ExecuteSQLReturnDataTable(SQL.SelectUnitsQuery, CommandType.Text, null);
ViewBag.Units = dtable;
return View(dtable);
}
我可以像这样从相应的视图访问它:
@using System.Data
@{
ViewBag.Title = "Platypus Report Scheduler";
DataTable ds = ViewBag.Units as DataTable;
var rows = from x in ds.AsEnumerable()
select new
{
unit = x.Field<string>("unit")
};
}
但是我需要引用多个数据集;我在控制器中试过这个:
public ActionResult Index()
{
DataTable dtable = new DataTable();
dtable = SQL.ExecuteSQLReturnDataTable(SQL.SelectUnitsQuery, CommandType.Text, null);
ViewBag.Units = dtable;
DataTable rpts = new DataTable();
rpts = SQL.ExecuteSQLReturnDataTable("select ReportName from ReportsLU", CommandType.Text, null);
ViewBag.Reports = rpts;
return View(dtable, rpts);
}
...但它不会编译;我得到,“参数 1:无法从 'System.Data.DataTable' 转换为 'string'” "dtable" 和 arg 2 ("rpts") 的相同错误。此外,“'System.Web.Mvc.Controller.View(string, string)' 的最佳重载方法匹配有一些无效参数”
解决这个问题的方法是什么? Return 来自控制器的通用数据表列表?直接在View中填充后续的DataTables?或者...???
您需要提供给 View 方法的是一个模型,它可以是一个 class 包含 2 个数据表:
public class DataModel
{
public DataTable DataTable1 { get; set; }
public DataTable DataTable2 { get; set; }
}
您遇到的错误说明您正在使用的重载 (View(string viewName, object model)
) 接受视图和模型的名称。
有两种解决方法。
首先是像您已经使用的那样使用 ViewBag
。第二种解决方案(在我个人看来是最好的)是创建一个新模型,其中包含您需要在视图中使用的所有数据。
第一次实施:
public ActionResult Index()
{
DataTable dtable = new DataTable();
dtable = SQL.ExecuteSQLReturnDataTable(SQL.SelectUnitsQuery, CommandType.Text, null);
ViewBag.Units = dtable;
DataTable rpts = new DataTable();
rpts = SQL.ExecuteSQLReturnDataTable("select ReportName from ReportsLU", CommandType.Text, null);
ViewBag.Reports = rpts;
return View();
}
在这种情况下,您不需要将 dtable
和 rpts
传递给 View
方法,因为值在 ViewBag
.
中
@using System.Data
@{
ViewBag.Title = "Platypus Report Scheduler";
DataTable ds = ViewBag.Units as DataTable;
DataTable ds2 = ViewBag.Reports as DataTable;
// Some other beautiful things
}
第二次实施:
public class YourModel {
public DataTable dt1 { get; set; }
public DataTable dt2 { get; set; }
public DataTable dt3 { get; set; }
// Other properties
}
public ActionResult Index()
{
YourModel model = new YourModel();
DataTable dtable = new DataTable();
dtable = SQL.ExecuteSQLReturnDataTable(SQL.SelectUnitsQuery, CommandType.Text, null);
model.dt1 = dtable;
DataTable rpts = new DataTable();
rpts = SQL.ExecuteSQLReturnDataTable("select ReportName from ReportsLU", CommandType.Text, null);
model.dt2 = rpts;
return View(model);
}
现在在视图中:
@model YourModel
@{
ViewBag.Title = "Platypus Report Scheduler";
// Retrive data in this way:
// Model.dt1
// Model.dt2
// Some other beautiful things
}
看来@model YourModel
是根本!
Return从控制器获取单个 DataTable 对我来说工作正常:
public ActionResult Index()
{
DataTable dtable = new DataTable();
dtable = SQL.ExecuteSQLReturnDataTable(SQL.SelectUnitsQuery, CommandType.Text, null);
ViewBag.Units = dtable;
return View(dtable);
}
我可以像这样从相应的视图访问它:
@using System.Data
@{
ViewBag.Title = "Platypus Report Scheduler";
DataTable ds = ViewBag.Units as DataTable;
var rows = from x in ds.AsEnumerable()
select new
{
unit = x.Field<string>("unit")
};
}
但是我需要引用多个数据集;我在控制器中试过这个:
public ActionResult Index()
{
DataTable dtable = new DataTable();
dtable = SQL.ExecuteSQLReturnDataTable(SQL.SelectUnitsQuery, CommandType.Text, null);
ViewBag.Units = dtable;
DataTable rpts = new DataTable();
rpts = SQL.ExecuteSQLReturnDataTable("select ReportName from ReportsLU", CommandType.Text, null);
ViewBag.Reports = rpts;
return View(dtable, rpts);
}
...但它不会编译;我得到,“参数 1:无法从 'System.Data.DataTable' 转换为 'string'” "dtable" 和 arg 2 ("rpts") 的相同错误。此外,“'System.Web.Mvc.Controller.View(string, string)' 的最佳重载方法匹配有一些无效参数”
解决这个问题的方法是什么? Return 来自控制器的通用数据表列表?直接在View中填充后续的DataTables?或者...???
您需要提供给 View 方法的是一个模型,它可以是一个 class 包含 2 个数据表:
public class DataModel
{
public DataTable DataTable1 { get; set; }
public DataTable DataTable2 { get; set; }
}
您遇到的错误说明您正在使用的重载 (View(string viewName, object model)
) 接受视图和模型的名称。
有两种解决方法。
首先是像您已经使用的那样使用 ViewBag
。第二种解决方案(在我个人看来是最好的)是创建一个新模型,其中包含您需要在视图中使用的所有数据。
第一次实施:
public ActionResult Index()
{
DataTable dtable = new DataTable();
dtable = SQL.ExecuteSQLReturnDataTable(SQL.SelectUnitsQuery, CommandType.Text, null);
ViewBag.Units = dtable;
DataTable rpts = new DataTable();
rpts = SQL.ExecuteSQLReturnDataTable("select ReportName from ReportsLU", CommandType.Text, null);
ViewBag.Reports = rpts;
return View();
}
在这种情况下,您不需要将 dtable
和 rpts
传递给 View
方法,因为值在 ViewBag
.
@using System.Data
@{
ViewBag.Title = "Platypus Report Scheduler";
DataTable ds = ViewBag.Units as DataTable;
DataTable ds2 = ViewBag.Reports as DataTable;
// Some other beautiful things
}
第二次实施:
public class YourModel {
public DataTable dt1 { get; set; }
public DataTable dt2 { get; set; }
public DataTable dt3 { get; set; }
// Other properties
}
public ActionResult Index()
{
YourModel model = new YourModel();
DataTable dtable = new DataTable();
dtable = SQL.ExecuteSQLReturnDataTable(SQL.SelectUnitsQuery, CommandType.Text, null);
model.dt1 = dtable;
DataTable rpts = new DataTable();
rpts = SQL.ExecuteSQLReturnDataTable("select ReportName from ReportsLU", CommandType.Text, null);
model.dt2 = rpts;
return View(model);
}
现在在视图中:
@model YourModel
@{
ViewBag.Title = "Platypus Report Scheduler";
// Retrive data in this way:
// Model.dt1
// Model.dt2
// Some other beautiful things
}
看来@model YourModel
是根本!