2
我有一個存儲過程,我試圖調用返回結果到我的視圖。我在我的模型文件夾中有一個類,我試圖從中調用它。調用MVC中的存儲過程
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();
}
我也有這個類,它看起來像一個數據庫上下文類,但是當我創建的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);
}
}
}
看來根據生成的上下文他已經導入了函數。 –
我添加了一張圖片顯示。感謝您的期待 –