Home > front end >  ASP.NET MVC / C# : how to display partial view data that belongs to another controller
ASP.NET MVC / C# : how to display partial view data that belongs to another controller

Time:09-21

I have this view called BlogsCategory.cshtml that belongs to CategoryController. Currently on this view the page loads a list of my Blog Categories the way its supposed to.

In addition to that data loading already I want to be able to also load data on that same view(BlogsCategory.cshtml) that belongs to another Controller called AdvertisementController whos data comes from another table called tblAdvertisement.

I did not want to create a database relationship between the BlogCategories Table and the Advertisement Table so I was wondering how in the world can I accomplish this via partial views that can load data from AdvertisementController on a view that belongs to CategoryController.

I have tried to add this @Html.Partial on that view but keep getting an error that states that the error below.

@model List<Systex.Models.tblCategory>

@Html.Partial("~/Views/Advertisement/Horizontal1.cshtml")

System.InvalidOperationException: 'The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Systex.Models.tblBlogsCategory]', but this dictionary requires a model item of type 'Systex.Models.tblAdvertisement'.'

Then the view that holds the piece of data that I want to load looks like this. This is a view of the AdvertisemenController that I am trying to call from the BlogsCategory.cshtml view.

Horizontal1.cshtml

@model Systex.Models.tblAdvertisement

//Display these 2 strings on the view called BlogsCategory.cshtml
@Model.BlogCategoriesListVerticalString1
@Model.BlogCategoriesListVerticalString2

CodePudding user response:

If you dont want to create a DB relationship, which if you are using EFCore I would recommend, then you will have to pass some identifying information to the controller such as an ID and look up the data then to map to the view model.

  • Related