簡體   English   中英

如何使用Entity Framework將模型數據傳遞到mvc中的布局頁面?

[英]How to Pass model data to layout page in mvc with Entity Framework?

我遇到了一個問題,我想在布局頁面上顯示模型的屬性。 這個答案的參考下,我嘗試按以下方式實現模型

公司簡介模型

[Table("tblCompany")]
    public abstract class CompanyProfile
    {
        [Key]
        public int CompanyId { get; set; }
        public string CompanyName { get; set; }
        public string CompanyLogo { get; set; }
        public string TitlePic { get; set; }
        public string CopyText { get; set; }
        public string RegText { get; set; }
    }

員工模型就像

[Table("tblEmployee")]
    public class Employee:CompanyProfile
    {
        [Key]
        public int EmpID { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public int DepartmentId { get; set; }
        public string EmpNo { get; set; }
    }

部門模型就像

[Table("tblDepartment")]
    public class Department:CompanyProfile
    {
        [Key]
        public int DeptID { get; set; }
        public string DeptName { get; set; }
    }

正如你所看到的,這兩個模型都是繼承自的

公司簡介

模型。 現在我的問題是當我試圖在我的控制器中訪問CompanyProfile模型時,它給了我一個錯誤

“無效的列名'CompanyId'。\\ r \\ n無效的列名'CompanyId'”

因為實體框架代表模型創建表並連接這些表並嘗試查找

CompanyId in tblEmployee

盡我所知。

SampleContext類就像

public class SampleContext:DbContext
    {
        public DbSet<Employee> empList { get; set; }
        public DbSet<Department> deptList { get; set; }
        public DbSet<CompanyProfile> Profile { get; set; }
    }

和HomeController一樣

SampleContext context=new SampleContext();
        public ActionResult Index()
        {
            CompanyProfile profile;
            if (Session["Profile"] == null)
            {
                Session["Profile"] = context.Profile.FirstOrDefault();
            }
            profile = (CompanyProfile)Session["Profile"];
            return View(profile);
        }

這是我的Layout.cshtml代碼

 @model ShareLayoutToContent.Models.CompanyProfile
<head>
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    <link rel="shortcut icon" href="@Url.Content(Model.TitlePic)">
</head>

現在,請你提供我犯錯誤的地方。 提前致謝。

查看您在問題中提供的鏈接,目的是創建一個總體上相同的main模型,例如:

public class BaseModel
{
    public CompanyProfile Profile { get; set; }
    public Employee Employee { get; set; }
    public Department Department { get; set; }
}

所以你的實體將是:

[Table("tblCompany")]
public class CompanyProfile
{
    [Key]
    public int CompanyId { get; set; }
    public string CompanyName { get; set; }
    public string CompanyLogo { get; set; }
    public string TitlePic { get; set; }
    public string CopyText { get; set; }
    public string RegText { get; set; }
}

[Table("tblEmployee")]
public class Employee
{
    [Key]
    public int EmpID { get; set; }
    public string Name { get; set; }
    public string Gender { get; set; }
    public int DepartmentId { get; set; }
    public string EmpNo { get; set; }
    public CompanyProfile profile { get; set; } //if needed
}

[Table("tblDepartment")]
public class Department
{
    [Key]
    public int DeptID { get; set; }
    public string DeptName { get; set; }
    public CompanyProfile profile { get; set; } //if needed
}

然后,您將能夠基於該BaseModel創建MainView。

HomeController的:

SampleContext context=new SampleContext();
public ActionResult Index()
{
    CompanyProfile profile;
    if (Session["Profile"] == null)
    {
       Session["Profile"] = context.Profile.FirstOrDefault();
    }
    profile = (CompanyProfile)Session["Profile"];
    return View(new BaseModel { Profile = profile });
}

查看或MainLayout:

@model ShareLayoutToContent.Models.BaseModel
<head>
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    <link rel="shortcut icon" href="@Url.Content(Model.Profile.TitlePic)">
</head>

@Model將包含BaseModel一個對象,因此您放入該對象的所有內容都可以在mainlayout和視圖中使用。 您只需要返回基於BaseModel的視圖。 這就是我在BaseModel類中聲明屬性的EmployeeDepartmentBaseModel
因此,在您需要執行的每個操作中:

return View(new BaseModel { /*property's you need */ });

我寧願建議你將CompanyProfile放在ViewBag中並忘記BaseModel比你可以保留原始視圖模型並在可以在例如Controller的構造函數中調用的過程中放置​​“幾乎靜態”值。

public class HomeController : Controller
{
    SampleContext context=new SampleContext();

    public HomeController()
    {
        ViewBag.CompanyProfile = context.Profile.FirstOrDefault();      
    }

    public ActionResult Index()
    {
        return View();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM