定义一个ViewModel类
public class DirectorVM
{
[Key]
public int DirectorID { get; set; }
[Display(Name = "Director Name")]
[DataType(DataType.Text)]
public string DirectorName { get; set; }
public int HometownID { get; set; }
}
在一个控制器中先定义一个dbcontext实例:
public class MoviesController : Controller
{
private MovieDBContext db = new MovieDBContext();
。。。。。
//在下面这个函数中调用db.Database.SqlQuery+sql语句返回List集合
public ActionResult ExportToExcelFromListDataSource(string hidForMovieGenre = "", string hidForDirector = "", string hidForMovieTitle = "")
{
try
{
List<DirectorVM> list = db.Database.SqlQuery<DirectorVM>("select DirectorID,DirectorName,HometownID from Directors").ToList();
return list;
}
。。。。。。
}