无法将数据 table 转换为 C# 中的列表
unble to convert data table to list in c#
我想将数据表数据显示到 html 中,以便将数据表转换为列表。但是获取错误不能隐式地将类型 'System.web.mvc.viewresult' 转换为 system.collection.genericlist
public ViewResult Report_trans_consumption(DateTime? fromDate, DateTime? toDate)
{
ViewBag.fromDate = fromDate;
ViewBag.toDate = toDate;
List<Trans_energycons_ReportModel> model = null;
using ("constr"))
{
con.Open();
SqlCommand cmd_get_transformer_consumption = new SqlCommand(@"SELECT
Date,units
FROM [Total_Power]) desc", con);
SqlDataAdapter da_get_trans_consumption = new SqlDataAdapter(cmd_get_transformer_consumption);
DataTable dt = new DataTable();
da_get_trans_consumption.Fill(dt);
Trans_energycons_ReportModel m =new Trans_energycons_ReportModel();
foreach (DataRow row in dt.Rows)
{
string deviceDate = row["Date"].ToString();
string units = row["Units"].ToString();
m.DeviceDate =Convert.ToDateTime(deviceDate);
m.Units =Convert.ToDouble(units);
model.Add(m);//getting error here
}
}
return View(model);
}
}
型号
public class Trans_energycons_ReportModel
{
public DateTime DeviceDate { get; set; }
public double Units { get; set; }
}
下一行无效
model = dt.AsEnumerable().ToList();
您必须根据数据 table 手动映射您的属性,例如
foreach(DataRow row in dt.Rows)
{
string deviceDate = row["DeviceDate"].ToString();
string units = row["Units"].ToString();
// convert your data and assign them in model and then use that model
}
编辑:
您的 return 类型是 List<Trans_energycons_ReportModel>
所以你应该 return return model;
而不是 return View(model);
把List<Trans_energycons_ReportModel>
改成ViewResult
或ActionResult
,像下面的代码:
public ViewResult Report_trans_consumption(DateTime? fromDate, DateTime? toDate)
{
....
}
我想将数据表数据显示到 html 中,以便将数据表转换为列表。但是获取错误不能隐式地将类型 'System.web.mvc.viewresult' 转换为 system.collection.genericlist
public ViewResult Report_trans_consumption(DateTime? fromDate, DateTime? toDate)
{
ViewBag.fromDate = fromDate;
ViewBag.toDate = toDate;
List<Trans_energycons_ReportModel> model = null;
using ("constr"))
{
con.Open();
SqlCommand cmd_get_transformer_consumption = new SqlCommand(@"SELECT
Date,units
FROM [Total_Power]) desc", con);
SqlDataAdapter da_get_trans_consumption = new SqlDataAdapter(cmd_get_transformer_consumption);
DataTable dt = new DataTable();
da_get_trans_consumption.Fill(dt);
Trans_energycons_ReportModel m =new Trans_energycons_ReportModel();
foreach (DataRow row in dt.Rows)
{
string deviceDate = row["Date"].ToString();
string units = row["Units"].ToString();
m.DeviceDate =Convert.ToDateTime(deviceDate);
m.Units =Convert.ToDouble(units);
model.Add(m);//getting error here
}
}
return View(model);
}
}
型号
public class Trans_energycons_ReportModel
{
public DateTime DeviceDate { get; set; }
public double Units { get; set; }
}
下一行无效
model = dt.AsEnumerable().ToList();
您必须根据数据 table 手动映射您的属性,例如
foreach(DataRow row in dt.Rows)
{
string deviceDate = row["DeviceDate"].ToString();
string units = row["Units"].ToString();
// convert your data and assign them in model and then use that model
}
编辑:
您的 return 类型是 List<Trans_energycons_ReportModel>
所以你应该 return return model;
而不是 return View(model);
把List<Trans_energycons_ReportModel>
改成ViewResult
或ActionResult
,像下面的代码:
public ViewResult Report_trans_consumption(DateTime? fromDate, DateTime? toDate)
{
....
}