USE [College] GO ALTER PROCEDURE [dbo].[Usp_Employee] @Mode TINYINT =NULL AS BEGIN IF @Mode=1 BEGIN SELECT EmplId,EmplName,EmpRole,Salary from [dbo].[Employee] ORDER BY EmplName; END ELSE IF @Mode=2 BEGIN SELECT EmplId,EmplName from [dbo].[Employee] ORDER BY EmplName; END END
namespace MVCQuestion.Models { using System; using System.Collections.Generic; public partial class Employee { public short EmplId { get; set; } public string EmplName { get; set; } public string EmpRole { get; set; } public Nullable<int> Salary { get; set; } } }
@model IEnumerable<MVCQuestion.Models.Employee> @{ ViewBag.Title = "Index"; } <h2>Employee Name and Role </h2> <table class="table"> <tr> <th> @Html.DisplayNameFor(model => model.EmplName) </th> <th> @Html.DisplayNameFor(model => model.EmpRole) </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.EmplName) </td> <td> @Html.DisplayFor(modelItem => item.EmpRole) </td> </tr> } </table>
using MVCQuestion.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MVCQuestion.Controllers { public class EmployeeController : Controller { CollegeEntities objDbContext = new CollegeEntities(); // GET: Employee public ActionResult Index() { var Query = "exec [dbo].Usp_Employee @Mode=2"; var Output = objDbContext.Database.SqlQuery<Employee>(Query).ToList<Employee>(); return View(Output); } } }