Example E3355
Visible to All Users

Grid View for ASP.NET MVC - How to adjust GridView to work with Stored Procedures (EntityFramework)

The example demonstrates how to use GridView with EntityFramework. For more informarion and implementation details, refer to the following article: How to adjust GridView to work with Stored Procedures (EntityFramework).

Files to Review

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

Controllers/HomeController.cs(vb)
C#
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using StoredProcedures.Models; using DevExpress.Web.Mvc; namespace StoredProcedures.Controllers { public class HomeController : Controller { public ActionResult Index() { ViewBag.Message = "The example demonstrates how to bind the grid to a Stored Procedure result.<br /> All changes that are cleared after 10 minutes."; /* Clean 10-min old records */ StoredProcedureEntities context = new StoredProcedureEntities(); DateTime oldDate = DateTime.Now.AddMinutes(-10.0); var supportTeamEntities = context.SupportTeams .OrderBy(i => i.Id) .Skip(4) .Where(i => i.ChangeDate < oldDate) .ToList(); foreach (SupportTeam item in supportTeamEntities) context.DeleteObject(item); context.SaveChanges(); return View(); } public ActionResult SupportGridPartial() { StoredProcedureEntities context = new StoredProcedureEntities(); var model = context.SelectSupportTeam(); return PartialView("SupportGridPartial", model); } [HttpPost] public ActionResult SupportGridInsert([ModelBinder(typeof(DevExpressEditorsBinder))] SelectSupportTeam_Result supportEngineer) { StoredProcedureEntities context = new StoredProcedureEntities(); var result = context.InsertSupportEngineer(supportEngineer.Name).First(); // inserted key value if (result.HasValue) TempData["message"] = "Inserted row key: " + result.Value.ToString(); var model = context.SelectSupportTeam(); return PartialView("SupportGridPartial", model); } [HttpPost] public ActionResult SupportGridDelete(Int32 Id) { StoredProcedureEntities context = new StoredProcedureEntities(); /* it is not allowed to delete some initial records */ Int32 foundItem = context.SupportTeams .Take(4) .Where(i => i.Id == Id) .Count(); if (foundItem != 0) TempData["message"] = "Error: It is not possible to delete test records"; else { var result = context.DeleteSupportEngineer(Id).First(); // a number of deleted rows (usually 1) if (result.HasValue) TempData["message"] = "A number of deleted rows: " + result.Value.ToString(); } var model = context.SelectSupportTeam(); return PartialView("SupportGridPartial", model); } } }
Views/Home/Index.cshtml
Razor
@{ ViewBag.Title = "How to adjust GridView to work with Stored Procedures (EntityFramework)"; } <h4>@Html.Raw(ViewBag.Message)</h4> <script type="text/javascript"> function grid_EndCallback(s, e) { if (typeof (s.cpMessage) != "undefined") { if (s.cpMessage.indexOf("Error") != -1) $('#message').attr("class", "errorMessage"); else $('#message').attr("class", "resultMessage"); $('#message').text(s.cpMessage) .fadeIn("slow"); delete (s.cpMessage); } } </script> @Html.Action("SupportGridPartial", "Home")
Views/Home/SupportGridPartial.cshtml
Razor
@model IEnumerable<StoredProcedures.Models.SelectSupportTeam_Result> @Html.DevExpress().GridView(settings => { settings.Name = "supportGrid"; settings.CallbackRouteValues = new { Controller = "Home", Action = "SupportGridPartial" }; settings.SettingsEditing.AddNewRowRouteValues = new { Controller = "Home", Action = "SupportGridInsert" }; settings.SettingsEditing.DeleteRowRouteValues = new { Controller = "Home", Action = "SupportGridDelete" }; settings.KeyFieldName = "Id"; settings.CommandColumn.Visible = true; settings.CommandColumn.NewButton.Visible = true; settings.CommandColumn.DeleteButton.Visible = true; settings.Columns.Add(column => { column.FieldName = "Id"; column.Width = 100; column.EditFormSettings.Visible = DefaultBoolean.False; }); settings.Columns.Add(column => { column.FieldName = "Name"; column.Width = 200; column.EditFormSettings.ColumnSpan = 2; }); settings.DataBound = (s, e) => { if (TempData.ContainsKey("message")) ((MVCxGridView)s).JSProperties["cpMessage"] = TempData["message"]; }; settings.SettingsLoadingPanel.Mode = GridViewLoadingPanelMode.ShowOnStatusBar; settings.Settings.ShowStatusBar = GridViewStatusBarMode.Visible; settings.SetStatusBarTemplateContent("<div id=\"message\" style=\"display: none;\"></div>"); settings.ClientSideEvents.EndCallback = "grid_EndCallback"; }).Bind(Model).GetHtml()

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.