从控制器返回单个 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:无法将“dtable”从“System.Data.DataTable”转换为“字符串”,而参数 2(“rpts”)也出现同样的错误。此外,“ 'System.Web.Mvc.Controller.View(string, string)' 的最佳重载方法匹配有一些无效参数”
解决这个问题的方法是什么?从控制器返回一个通用的 DataTable 列表?直接在View中填充后续的DataTables?或者...???