I have a PartialView with a Model of type Machine
that renders a row in a table.
@model GPT.Core.Models.Machine
<tr>
@*
some html
*@
</tr>
This partial view is working fine when rendered in a loop in the parent view like this:
@foreach (var machine in pl.Machines)
{
// some code
// ...
await Html.RenderPartialAsync("_MachineTableRow", machine);
}
I'm using a AJAX-Request on the client to update some data and want to update the corresponing data row as well thereafter. So my AJAX-request calls
public async Task<PartialViewResult> OnPostAnalyzeMachineJournalAsync()
{
// some code
// ...
var machine = GetUpdateMachinePseudoMethod();
return Partial("_MachineTableRow", machine);
}
But this throws an exception:
System.InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'GPT.Core.Models.Machine', but this ViewDataDictionary instance requires a model item of type 'GPT.Web.Pages.IndexModel'.
I'm not sure to which ViewDataDictionary
this exception is refering. But even if it doesn't make sense to me and just out of curiosity, I provided the IndexModel
that is mentioned by the exception.
await Html.RenderPartialAsync("_MachineTableRow", this);
Then there is anoter exception telling my exactly the same thing the other way round, which leaves me completely confused now:
System.InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'GPT.Web.Pages.IndexModel', but this ViewDataDictionary instance requires a model item of type 'GPT.Core.Models.Machine'.
Can anyone tell why it isn't working when I pass a Machine
instance like it is defined in the partial cshtml file and like it is also mentioned in the second exception? What am I missing here?
Ok, further investigation revealed this:
Seems this is really a Bug in .netcore 2.2 and has been fixed in .netcore 3.0 I'll give the RC version a try and will report back if this was successful.
Edit: After updating the target framework to .netcore 3.0, the code works as expected and accepts the provided model.