在MVC中调用存储过程

时间:2016-01-21 15:15:29

标签: c# sql-server asp.net-mvc asp.net-mvc-4 stored-procedures

Menulistfunctionimport我有一个存储过程,我试图调用将结果返回给我的视图。我在我的模型文件夹中有一个类,我试图从中调用它。

 public  class MasterMenu
    {

        public  List<USP_MenuList_ForUser_G_Result> GetMenus(int userId)
        {


            List<USP_MenuList_ForUser_G_Result> GetMenuListForUser = null;
            using (MenuEntities dataContext = new MenuEntities())
            {
                GetMenuListForUser = dataContext.USP_MenuList_ForUser_G(56367).ToList();
                return GetMenuListForUser;
            }
        }

我试图在我的视野中达到它

<body>

    <div id="header">
        <div id="logo">
        </div>

    </div>
    @if (Model.GetMenus(56367) != null && Model.GetMenus(56367).Count > 0)
    { }



    @RenderBody()
</body>

我不知道在我的控制器中要做什么来达到存储过程。该过程采用一个参数,即用户ID,它需要访问的所有表都在我的EDMX中。这是我的控制器的样子。

public class MasterMenuController : Controller
    {
        private MenuEntities menuEntities = new MenuEntities();
}

我也有这个看起来像db上下文类的类,但在创建EDMX时自动生成。

//------------------------------------------------------------------------------
// <auto-generated>
//     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.
// </auto-generated>
//------------------------------------------------------------------------------

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<tblAgent> tblAgents { get; set; }
        public virtual DbSet<tblMenu> tblMenus { get; set; }
        public virtual DbSet<tblQualifier> tblQualifiers { get; set; }
        public virtual DbSet<tblRoleMenuMapping> tblRoleMenuMappings { get; set; }
        public virtual DbSet<tblRole> tblRoles { get; set; }
        public virtual DbSet<tblUserRoleMapping> tblUserRoleMappings { get; set; }
        public virtual DbSet<tblUsersLogin> tblUsersLogins { get; set; }
        public virtual DbSet<tblUserMenuMapping> tblUserMenuMappings { get; set; }

        public virtual ObjectResult<USP_MenuList_ForUser_G_Result> USP_MenuList_ForUser_G(Nullable<int> userID)
        {
            var userIDParameter = userID.HasValue ?
                new ObjectParameter("UserID", userID) :
                new ObjectParameter("UserID", typeof(int));

            return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<USP_MenuList_ForUser_G_Result>("USP_MenuList_ForUser_G", userIDParameter);
        }
    }
}

MenuModelExplorer

MenuEntities

1 个答案:

答案 0 :(得分:2)

您的控制器必须使其可用于视图,例如传递到强类型视图或ViewBag中的模型的一部分。由于您似乎是在全局布局或部分视图中执行此操作,因为它是一个始终可见的菜单,您可以考虑将代码包装在一个全局过滤器中添加到ViewBag中您的global.asax中的网站。