我一直在尝试显示一个 MySQL 表,但它告诉我它需要一个数据源,所以我已经尝试过grid.DataSource = UserList;
等等。似乎没有任何效果。
[HttpPost]
public ActionResult Search(Models.SearchModel user)
{
List<Models.SearchModel> UserList = new List<Models.SearchModel>();
MySqlConnection connection = DBConnect.getconnection(); // setting connection to database
MySqlCommand cmd = new MySqlCommand("GetUsers", connection); // search for procedure called "GetData"
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new MySqlParameter("?search", MySqlDbType.VarChar)); // search parameters, if not looking for anythinf gets all the data
cmd.Parameters["?search"].Value = "%" + "" + "%";
cmd.Parameters["?search"].Direction = ParameterDirection.Input;
MySqlDataReader dr = cmd.ExecuteReader(); // telling program to read Data
while (dr.Read())
{
int id = Convert.ToInt16(dr["ID"]);
string user_name = Convert.ToString(dr["user_name"]); // converting data to a string
Models.SearchModel UserMod = new Models.SearchModel(id, user_name);
UserList.Add(UserMod);
}
dr.Close(); // close
DBConnect.CloseConnection(connection); // closes connection
return View("Search", UserList);
}
在 cshtml 页面上,我有以下代码:
@model IEnumerable<AOSExpress.Models.SearchModel>
<div>
@{
var grid = new WebGrid(@Model);
}
@grid.GetHtml()
</div>