Controllers中
public ActionResult Index() { OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["connstr"].ToString()); conn.Open(); DataTable datatable = new DataTable(); OleDbCommand comm = new OleDbCommand("select * from UserList",conn); OleDbDataAdapter dar = new OleDbDataAdapter(comm); dar.Fill(datatable); conn.Close(); conn.Dispose(); Model.UserList userlist = new UserList(); List<Model.UserList> list_m = new List<UserList>(); list_m =(List<Model.UserList>)Model.ModelConvertHelper<Model.UserList>.ConvertToModel(datatable); return View(list_m); //list_m是list<model>的数据 }
view中要怎么显示list_m的值
@model IEnumerable<MVC.Model.UserList> //这个一直认不到
@{
foreach (var item in Model)
{
Response.Write(item); //这里一直读不到值,
}
}
不一定要通过传递Model给视图,也可以通过ViewBag或ViewData或TempData,比如
public ActionResult Index() {
ViewData.Add("UserList",list);
return View();
}
@model MVC.Model.UserList
@{
var list=ViewData["UserList"] as List<MVC.Model.UserList>;
foreach (var item in list)
{
Response.Write(item.Name);
}
}
非常感谢!!!
@0591zpw: 哥们,你园豆好多,怎么来的?
@king2003: 以前回答问题来的啊
加个断点看看Datatable有没有成功转化成List
亲,有成功转化成list的,
for(int i=0;i<list_m.count;i++)
{
Response.Write(list_m[i].UserName);//这样可以正常输出;
}
conn.Dispose(); 去掉试试
还是不行,list有正常读到数据
Response.Write(item.UserName);
这样能读到么?
你在新建视图的时候有没有选择强类型视图?
亲,没有