Reputation: 54
When I list the data from the table(Tips) of Type Model as a partialView I get this error .
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List
1[RealEstateAspNetCore3._1.Models.Advertisement]', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.IEnumerable
1[RealEstateAspNetCore3._1.Models.Tip]'.
The table Tip
(Type) contains StatusId
as a foreign key.
Tip.cs model class:
[Key]
public int TypeId { get; set; }
public string TypeName { get; set; }
public int StatusId { get; set; }
public virtual Status Status { get; set; }
Status.cs
[Key]
public int StatusId { get; set; }
public string StatusName { get; set; }
public List<Tip> Tips { get; set; }
advertisement.cs
[Key]
public int AdvId { get; set; }
public string Description { get; set; }
public int StatusId { get; set; }
public int TypeId { get; set; }
[ForeignKey("TypeId")]
public Tip Tip { get; set; }
HomeController
public PartialViewResult StatusName1()
{
string statusname1 = _context.Tips.Where(i => i.StatusId == 1)
.FirstOrDefault()
.ToString();
return PartialView(statusname1);
}
Partial view StatusName1:
@model RealEstateAspNetCore3._1.Models.Tip
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button">
@Model.Status.StatusName <span class="caret"></span> </a>
Index.cshtml
in Index I list the advertisement tables and works well and i used model like below
@model IEnumerable<RealEstateAspNetCore3._1.Models.Advertisement>
in layout after i call the partial above RenderBoy like this i get error
<partial name="StatusName1" />
I spent two days trying to solve this, and the error stayed the same.
Thank you
Upvotes: 0
Views: 2117
Reputation: 336
If you're calling the partial view from _Layout.cshtml when you need to reference the full path of the view:
<partial name="~/Views/Home/StatusName1.cshtml" />
Replace Views/Home with whatever the folder names are in your project. See more here: https://learn.microsoft.com/en-us/aspnet/core/mvc/views/partial?view=aspnetcore-5.0#partial-tag-helper
There are model and a whole host of other issues in the snippets you've provided - I think the answer from @pritom-sarkar has addressed most of those
Upvotes: 1
Reputation: 2252
I assumed that your partial view name is StatusName1
change your code like below:-
public IActionResult StatusName1()
{
var status1 = _context.Tips.Where(i => i.StatusId == 1)
.FirstOrDefault();
return PartialView("StatusName1",status1);
}
It will resolve your issue.
UPDATE
in MVC core You need to use ViewComponent instead partial view.
create a folder with the name Components
The class must inherit from the ViewComponent. I will give this component class name CategoryWiseMenu
namespace DigitalShop.Components
{
public class CategoryWiseMenu:ViewComponent
{
private readonly ApplicationDbContext _db;
public CategoryWiseMenu(ApplicationDbContext db)
{
_db = db;
}
public IViewComponentResult Invoke()
{
var c = _db.Tips.Where(i => i.StatusId == 1)
.FirstOrDefault();
return View("Default.cshtml",c);
}
}
}
~/Views/Shared/Components/Default.cshtml
Create Default.cshtml
in Components folder like above path.
Demo Default.cshtml
<ul class="navbar-nav flex-grow-1">
@foreach (var rr in Model)
{
<li class="nav-item text-dark">
<a asp-controller="" asp-action=""
class="nav-link text-dark">@rr.TypeName</a>
</li>
}
</ul>
now include below code in your layout page.
@await Component.InvokeAsync("CategoryWiseMenu")
now it will works fine which you want.
Upvotes: 0