asp.net 中mvc3将数据如何 绑定到sqldatareader或dataset public ActionResult Index(MessageInfo model) { SqlConnection cn = new SqlConnection(strConn); cn.Open(); SqlCommand cmd = new SqlCommand("select * from Message", cn); SqlDataReader dr = cmd.ExecuteReader(); // Models.MessageInfo ms = new Models.MessageInfo(); if (dr.Read()) { //ViewData["id"]=dr["ID"]; //mod
public ActionResult Index(MessageInfo model) { SqlConnection cn = new SqlConnection(strConn); cn.Open(); SqlCommand cmd = new SqlCommand("select * from Message", cn); SqlDataReader dr = cmd.ExecuteReader(); List<Models.MessageInfo> ms = new List<Models.MessageInfo>(); while (dr.Read()) { Models.MessageInfo data = new Models.MessageInfo(); data.MessageName = (string)dr["MessageName"]; data.MessageContent = (string)dr["MessageContent"]; data.qq = (string)dr["qq"]; data.datetime = (DateTime)dr["Datetime"]; ms.Add(data); } cn.Close(); return View(ms);
}
------解决方案-------------------- 自定一个MODEL类
public class ModelIndex{ public List<T> List{get;set;} public List<T> List2{get;set;} public string Title{get;set;} }
这样就可以了,是MVC中最好的办法。 @Model.Title @foreach(var r in Model.List2) @foreach(var r in Model.List) @foreach(var r in Model.List)
------解决方案-------------------- mvc里面没有你想要的邦定
------解决方案--------------------