Simple fix for 'Cannot bind source type Umbraco.Web.Models.RenderModel to model type xx'

When calling @Html.Partial("MyPartialName", myModel), you might sometimes run into 'Cannot bind source type Umbraco.Web.Models.RenderModel to model type xx', it turns out that the solution to this issue might be a little simpler than you first thought.

What you shouldn't do (not the fix):

Your first thought might be to make your model inherit from RenderModel, but if you're doing everything the same as all your other partials and they don't have their models extend RenderModel, then chances are, that's not the issue. Don't do it. It'll be more work than you think to implement, you'll spend hours on it and it'll turn out that you need to remove it all again.

public class MyModel : RenderModel { ... all the other things that go with setting up a render model ... }

What's wrong?

If like me, you have a helper function to get your model for this particular instance, your issues could become obfuscated because that function might not be returning what you think it is. The error is a misleading one at best.

The Fix:

Make sure your model isn't null before using it as a parameter for Html.Partial. Seriously. The error is misleading because it reports your model is of the wrong type, technically it's true, but that's only because it's null. You could try something like the below:

MyModel objModel = SomeNamespace.GetModelFromPlace(myParams); // Might return null

if (objModel != null) // Only render partial of there's something to work with.
{
    @Html.Partial("MyPartial", myModel)
}

Or failing that, if you need to render the partial:

MyModel objModel = SomeNamespace.GetModelFromPlace(myParams); // Might return null.

if (objModel == null) // If model is null, just use a blank one.
{
    obj|Model = new MyModel();
}

@Html.Partial("MyPartial", myModel) // Sorted

Simple isn't it? Don't let the 'Cannot bind source type Umbraco.Web.Models.RenderModel to model type xx' error fool you.

Still not working? See other reasons it might not be working:

This guy was returning a view from a controller with the same error.


Published at

Tags: Umbraco

Luke Alderton

Comments

dsad
can u please o please bite me
09/03/2018
Post a comment
Sit tight...
We're adding your comment.
Thank you.
Your comment has been added.
There's been a problem.
Please try again later.
Share with
Tags
Latest Comments
By DavidassuG on MVC forms in Umbraco
12 Jul 2018, 10:54 AM
By Luke on Setting up tables in SQLite with a primary key on a Xamarin Forms app
11 Jul 2018, 09:18 AM
By Travis on Setting up tables in SQLite with a primary key on a Xamarin Forms app
7 Jul 2018, 15:44 PM
By Luke on Auto/Custom height on Xamarin Forms WebView for Android and iOS
15 Jun 2018, 09:43 AM
By vikki on Auto/Custom height on Xamarin Forms WebView for Android and iOS
14 Jun 2018, 13:35 PM
By jimbo on JQuery Unobtrusive Validation for dynamically created elements
13 Jun 2018, 17:13 PM
By ScottKem on MVC forms in Umbraco
8 Jun 2018, 10:29 AM
By Alaa on java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path
3 Jun 2018, 20:16 PM
Categories
App Development
Event
Game Development
Mapping
Modelling
Programming
Review
Robotics
Tutorial
Web Development