title: Calling a stored procedure in MVC
body: <p><a href="https://i.stack.imgur.com/U0ex7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/U0ex7.png" alt="Menulistfunctionimport"></a>I have a stored procedure that i am trying to call to return a result to my view. I have a class in my models folder that i am trying to call it from. </p>
  
  <pre><code> public  class MasterMenu
      {
  
          public  List&lt;USP_MenuList_ForUser_G_Result&gt; GetMenus(int userId)
          {
  
  
              List&lt;USP_MenuList_ForUser_G_Result&gt; GetMenuListForUser = null;
              using (MenuEntities dataContext = new MenuEntities())
              {
                  GetMenuListForUser = dataContext.USP_MenuList_ForUser_G(56367).ToList();
                  return GetMenuListForUser;
              }
          }
  </code></pre>
  
  <p>I was trying to reach it in my view like so </p>
  
  <pre><code>&lt;body&gt;
  
      &lt;div id="header"&gt;
          &lt;div id="logo"&gt;
          &lt;/div&gt;
  
      &lt;/div&gt;
      @if (Model.GetMenus(56367) != null &amp;&amp; Model.GetMenus(56367).Count &gt; 0)
      { }
  
  
  
      @RenderBody()
  &lt;/body&gt;
  </code></pre>
  
  <p>I was not sure what to do in my controller to reach the stored procedure. The procedure takes one parameter which is User ID and all of the tables that it needs to access are in my EDMX. Here is what my controller looks like. </p>
  
  <pre><code>public class MasterMenuController : Controller
      {
          private MenuEntities menuEntities = new MenuEntities();
  }
  </code></pre>
  
  <p>I also have this class which looks like a db context class but was automatically generated when i created the EDMX.</p>
  
  <pre><code>//------------------------------------------------------------------------------
  // &lt;auto-generated&gt;
  //     This code was generated from a template.
  //
  //     Manual changes to this file may cause unexpected behavior in your application.
  //     Manual changes to this file will be overwritten if the code is regenerated.
  // &lt;/auto-generated&gt;
  //------------------------------------------------------------------------------
  
  namespace Adds.Areas.Mvc.Menu.Models
  {
      using System;
      using System.Data.Entity;
      using System.Data.Entity.Infrastructure;
      using System.Data.Entity.Core.Objects;
      using System.Linq;
  
      public partial class MenuEntities : DbContext
      {
          public MenuEntities()
              : base("name=MenuEntities")
          {
          }
  
          protected override void OnModelCreating(DbModelBuilder modelBuilder)
          {
              throw new UnintentionalCodeFirstException();
          }
  
          public virtual DbSet&lt;tblAgent&gt; tblAgents { get; set; }
          public virtual DbSet&lt;tblMenu&gt; tblMenus { get; set; }
          public virtual DbSet&lt;tblQualifier&gt; tblQualifiers { get; set; }
          public virtual DbSet&lt;tblRoleMenuMapping&gt; tblRoleMenuMappings { get; set; }
          public virtual DbSet&lt;tblRole&gt; tblRoles { get; set; }
          public virtual DbSet&lt;tblUserRoleMapping&gt; tblUserRoleMappings { get; set; }
          public virtual DbSet&lt;tblUsersLogin&gt; tblUsersLogins { get; set; }
          public virtual DbSet&lt;tblUserMenuMapping&gt; tblUserMenuMappings { get; set; }
  
          public virtual ObjectResult&lt;USP_MenuList_ForUser_G_Result&gt; USP_MenuList_ForUser_G(Nullable&lt;int&gt; userID)
          {
              var userIDParameter = userID.HasValue ?
                  new ObjectParameter("UserID", userID) :
                  new ObjectParameter("UserID", typeof(int));
  
              return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction&lt;USP_MenuList_ForUser_G_Result&gt;("USP_MenuList_ForUser_G", userIDParameter);
          }
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/ItDNt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ItDNt.png" alt="MenuModelExplorer"></a></p>
  
  <p><a href="https://i.stack.imgur.com/sr8D9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sr8D9.png" alt="MenuEntities"></a></p>