我有一個名為 BlogsCategory.cshtml 的視圖,屬于 CategoryController。目前在這個視圖中,頁面以其應有的方式加載我的博客類別串列。
除了已經加載的資料,我還希望能夠在同一個視圖(BlogsCategory.cshtml)上加載資料,該視圖屬于另一個名為AdvertisementController的控制器,其資料來自另一個名為tblAdvertisement的表格。
我不想在BlogCategories表和Advertisement表之間建立資料庫關系,所以我想知道如何通過部分視圖來完成這個任務,在屬于CategoryController的視圖中加載AdvertisementController的資料。
我試著在該視圖上添加這個@Html.Partial,但一直得到一個錯誤,說是下面的錯誤。
@model List<Systex.Models.tblCategory>
@Html.Partial("~/Views/Advertisement/Horizontal1.cshtml"/span>)
System.InvalidOperationException: '傳入字典的模型項是'System.Collections.Generic.List`1[Systex.Models.tblBlogsCategory]'型別的,但這個字典需要'Systex.Models.tblAdvertisement'型別的模型項。'
那么持有我想加載的資料的視圖看起來像這樣。這是我想從 BlogsCategory.cshtml 視圖中呼叫的 AdvertisemenController 的視圖。
Horizontal1.cshtml
@model Systex.Models.tblAdvertisement
/Display these 2 strings on the view called BlogsCategory.cshtml
@Model.BlogCategoriesListVerticalString1
@Model.BlogCategoriesListVerticalString2
uj5u.com熱心網友回復:
如果你不想創建一個資料庫關系,如果你使用EFCore,我建議你這樣做,那么你將不得不向控制器傳遞一些識別資訊,如ID,然后查找資料以映射到視圖模型。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/324064.html
標籤:
下一篇:注冊表的服務器端或客戶端過濾?
