0

I have this model in my view:

@model MGDanismanlik.Controllers.SettingsController.WorkCampClas

I call this partial view bottom of the view:

@Html.Partial("MonthlyResultPartial")  

This is the model I use in MonthlyResultPartial:

@model IEnumerable<MGDanismanlik.Controllers.SettingsController.WorkCampaignResultClass>

But when I run it I got this error.

The model item passed into the dictionary is of type 'MGDanismanlik.Controllers.SettingsController+WorkCampClas', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MGDanismanlik.Controllers.SettingsController+WorkCampaignResultClass]'.

What's the problem? I use different model in my partial view. How can I solve this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    Pass the model in as a parameter: @Html.Partial("MontlyResultPartial", model) – GH DevOps Apr 25 '23 at 16:33
  • Your Partial View is expecting an object of type IEnumerable but your View is passing in an object of type WorkCampaignResultClass. You'll need to adjust either your View or Partial View so that the types align. – bschellekens Apr 25 '23 at 16:37
  • @bschellekens why my view and my partial view gotta have same models? Arent they seperated views? – Mert Çakar Apr 25 '23 at 16:43
  • Your view and partial view do NOT have to use the same model, BUT if you do NOT explicitly pass a reference to the model for the partial, then it is implied that the partial uses the same model. See the comment from @GHDevOps for the call signature that you want to use. – David Tansey Apr 25 '23 at 16:53
  • I tried it but the model returned null. I have this --> public PartialViewResult MonthlyResultPartial() {...} in my controller. I send model in this method to my partial view. I couldnt understand whats the problem. – Mert Çakar Apr 25 '23 at 17:02
  • Inside WorkCampClas add a IEumerable property and then pass in that property into your partial. The partial model must be a property in the parent model. – GH DevOps Apr 25 '23 at 17:45
  • @GHDevOps I think I couldnt explain myself correctly. However I solved it using Html.Action("MontlyResultPartial", "Settings") with this instead Html.Partial. I didnt change my models or send a model in the method. – Mert Çakar Apr 25 '23 at 18:31

0 Answers0