日期:2014-05-17  浏览次数:20500 次

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

  //ViewData["MessageName"] = ;
  //ViewData["MessageContent"] = dr["MessageContent"];
  //ViewData["qq"] = dr["qq"];
  //ViewData["Datetime"] = dr["Datetime"];
  // ms.id = (int)dr["ID"];
  //ms.MessageName = (string)dr["MessageName"];
  //ms.MessageContent = (string)dr["MessageContent"];
  //ms.qq = (string)dr["qq"];
  //ms.datetime = (DateTime)dr["Datetime"];
  model.MessageName = (string)dr["MessageName"];
  model.MessageContent = (string)dr["MessageContent"];
  model.qq = (string)dr["qq"];
  model.datetime = (DateTime)dr["Datetime"];
  }
  cn.Close();
  return View(model);
   
  }
貌似不像.aspx那样的,好像不能这么绑定,求解,谢谢了,最好能给段sql的绑定源代码,不要linq的

------解决方案--------------------
@model List<MessageInfo > VIEW页面


后台


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里面没有你想要的邦定
------解决方案--------------------
探讨
自定一个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.……