Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel', but this dictionary requires a model item of type 'TestModel'

I was trying to add a new MVC template to an existing site that I've been developing for the last few months. I have a couple custom controllers/models setup that are working fine. Today I tried to add another new template and I've run into this error that I can't get past. Umbraco version is 7.04.

First, the error:

http://i.imgur.com/iH6cmUD.png

~/Views/MVCTest.cshtml

@inherits Umbraco.Web.Mvc.UmbracoViewPage<TestModel>
@{
  Layout = null;
}
<h1>Hello, World!</h1>

~/App_Code/Test/TestController.cs

using System.Web.Mvc;
using Umbraco.Web.Models;
using Umbraco.Web.Mvc;

public class TestController : RenderMvcController {
  public override ActionResult Index(RenderModel model) {
    var test = new TestModel(model.Content, model.CurrentCulture);
    return base.Index(test);
  }
}

~/App_Code/Test/TestModel.cs

using System.Globalization;
using Umbraco.Core.Models;
using Umbraco.Web.Models;

public class TestModel : RenderModel {
  public TestModel(IPublishedContent content, CultureInfo culture)
    : base(content, culture) {

    }

}

Anybody see any obvious issues? It's really strange because I have essentially the same exact code working for some other custom models and controllers so I'm totally confused as to why any new types give me this error.

like image 446
Nathan Avatar asked Mar 19 '23 15:03

Nathan


1 Answers

Your code is almost right, the problem is that your view is called MVCtest.cshtml because presumably your document type is called MVCtest, however your Controller is just called TestController and not MVCTestController.

Umbraco's routing is quite specific in that it will route all traffic for a specific document type via a controller with a matching name.

Also, you don't need the return base.Index(test);, just return CurrentTemplate(test);

An additional point is that Umbraco will by default attempt to route via the Index action unless you have actions that match the name of your templates. So, you could have multiple templates each with separate actions.

like image 124
Digbyswift Avatar answered Mar 21 '23 05:03

Digbyswift