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

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.