Define a ViewModel class
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;}
}
Define a DbContext instance first in a controller:
public class Moviescontroller:controller
{
Private Moviedbcontext db = new Moviedbcontext ();
。。。。。
The DB is called in the following function. Database.sqlquery+sql statement returns the list collection
Public ActionResult Exporttoexcelfromlistdatasource (String hidformoviegenre = "", String hidfordirector = "", String HIDF Ormovietitle = "")
{
Try
{
list<directorvm> list = db. Database.sqlquery<directorvm> ("Select Directorid,directorname,hometownid from directors"). ToList ();
return list;
}
。。。。。。
}
MVC5 DBContext.Database.SqlQuery Gets the collection of objects into the ViewModel collection (you can use this method to give the @model source as the Foreground View page cshtml page)?? Pending verification