Multi-Table Base Select View in MVC 5.0 With Database First Approach

Introduction

In this article, we will learn how you can use multiple table bases by selecting View in MVC 5.0. Select View is basically a select query , which selects your data from your database.

Now, let's start.

Step 1

Here, we will create our database demo2 and two tables (student,college).

MVC

Step 2

Now, we will create our select view, which selects the data from both the tables. 

  1. CREATE VIEW [dbo].[stud_detaile] AS SELECT id,name,rollno,classs,subjects,fees,collegename from student join college on student.collegeId=college.collegeId;   

MVC

Step 3

Now, we will open Soluction Explorer and create a Home controller. After that select-> Model and Right click and select Add. Afterwards, select New item.

MVC

Afterwards, select ADO.NET Entity Data Model and click Add button.

MVC

Afterwards, an Entity Data Model Wizard opens and select Generate from database option. Subsequenty, click Next option. Here, before creating new connection, click Next option and choose an Entity Framework 5.0. Click Next and select your Views, followed by stud_detaile and click Finish button.

MVC

In this way, we have created our model.

MVC

Now, we need to double click on Model1.edmx.

MVC

Now, three classes are automatically created. The first two classes represents table and third class stud_detail represents our select view. 

Step 4

In this step, open Home controller. Give the reference of Model and create the object of connection class. 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using Webtest1.Models;  
  7. namespace Webtest1.Controllers {  
  8.     public class HomeController: Controller {  
  9.         demo2Entities1 ds = new demo2Entities1();  
  10.         public ActionResult Index() {  
  11.             return View();  
  12.         }  
  13.     }  
  14. }  
  15. Now we are create show_detaile() method.  
  16.     [HttpGet]  
  17. public ActionResult show_detail1() {  
  18.     var data = ds.stud_detaile.ToList();  
  19.     return View(data);  
  20. }   

Add new view.

MVC

MVC

Now, your view is ready.

MVC

Now, build the solution and run it. Afterwards, your output is ready.

MVC


Similar Articles