我正在尝试在 MVC 中使用以下代码,这是我到目前为止所做的:
public class MoviesModel
{
public int Id { get; set; }
public string MovieName { get; set; }
public string Actor { get; set; }
public int Year { get; set; }
}
控制器
public MoviesModel Index()
{
MoviesModel myModel;
string connectionString =
"";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand com = con.CreateCommand();
com.CommandText = "SELECT [ID] ,[MovieName] ,[Actor] ,[Year] FROM [dbo].[Movies]";
con.Open();
using (SqlDataReader reader = com.ExecuteReader())
{
while (reader.Read())
{
myModel = new MoviesModel
{
myModel.Id = Convert.ToInt32(reader[0]),
myModel.MovieName = reader[1].ToString(),
myModel.Actor = reader[2].ToString(),
myModel.Year = Convert.ToInt32(reader[3])
};
}
}
con.Close();
return myModel;
}
但我得到以下错误
cannot initialize class with a collection because it does not implement System.Collection.IEnumerable