Quantcast
Channel: ASPNETWebStack Issue Tracker Rss Feed
Viewing all articles
Browse latest Browse all 7215

Edited Issue: Poor error message from Html.Partial(...) overrides [423]

$
0
0
Scenario:
You have a view model that contains a nested view model:
public class ParentVm
{
public ChildVm ChildVm { get; set; }
}

In your view, you render a partial view and you pass it the child vm as it's model:

@Html.Partial("somepartialview", Model.ChildVm)

If ChildVm is null, this will throw an InvalidOperationException: "The model item passed into the dictionary is of type 'ParentVm', but this dictionary requires a model item of type 'ChildVm'.

This error leads to a huge amount of confusion. It describes what MVC did under the covers, NOT what I did in my code.

What's happening is MVC has a number of overrides for Html.Partial that all call the same final method, System.Web.Mvc.HtmlHelper.RenderPartialInternal. RenderPartialInternal checks if model is null and if it is uses the existing ViewData.

I think this is a confusing implementation. If I explicitly use a method overload that specifies a model, but I pass in null, I *should* get an exception. But the exception shouldn't be that I provided the wrong view model type, when what I actually did was pass in null!

Viewing all articles
Browse latest Browse all 7215

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>