Bhawna Jain
Bhawna Jain

Reputation: 759

route hijacking/Cannot bind source type Umbraco.Web.Models.RenderModel to model type

I am trying to make a strongly typed view in umbraco..but I am stuck at a point where I am getting this error.

Cannot bind source type Umbraco.Web.Models.RenderModel to model type umbraco_demo.Model.HomeModel.

My model class:

public class HomeModel : RenderModel
{
    //Standard Model Pass Through
    public HomeModel(IPublishedContent content) : base(UmbracoContext.Current.PublishedContentRequest.PublishedContent, UmbracoContext.Current.PublishedContentRequest.Culture) { }

    //Custom properties here...
    public string MyProperty1 { get; set; }
    public string MyProperty2 { get; set; }
} 

My Controller

 public class HomeController : Umbraco.Web.Mvc.RenderMvcController
{
    public ActionResult HomeModel(RenderModel model)
    {
        //we will create a custom model
        var myCustomModel = new HomeModel(model.Content);
        myCustomModel.MyProperty1 = DateTime.Today.ToString();
        //TODO: assign some values to the custom model...

        return CurrentTemplate(myCustomModel);
    }
}

View in umbraco:

@using umbraco_demo.Model
@inherits UmbracoViewPage<HomeModel>
@{
    Layout = "Master.cshtml";
}
@{Model.Content.GetPropertyValue<string>("MyProperty1");}

Also I have a document type in umbraco with name Home having above template.

I even referred this post on umbraco forum But still getting same error.

Upvotes: 0

Views: 2829

Answers (2)

Lloyd Chakandinakira
Lloyd Chakandinakira

Reputation: 11

Also make sure that your controller name is the same as the document type that you are currently working with

Upvotes: 1

harvzor
harvzor

Reputation: 2908

Fixing the controller

Change your controller method name to:

public override ActionResult Index(RenderModel model)
{
    ...
}

The Umbraco.Web.Mvc.RenderMvcController has a method also called Index which you need to override. It will probably otherwise use that virtual method as the default (which will return a different model type).

Index is also the default method will be called when a page is loaded with no (GET or POST) params.

If you used a debugger on your controller, you should have been able to see your custom method was not being hit on page load.

Updating the model

I haven't used your specific implementation of the model. It may be worth changing the constructor to:

public HomeModel(IPublishedContent content, CultureInfo culture) : base (content, culture) { }

And changing the instantiation in the controller of this model to:

var myCustomModel = new HomeModel(model.Content, System.Globalization.CultureInfo.CurrentCulture);

View changes

The line in your view where you're trying to get the property MyProperty1 is probably wrong. I assume that this property does not exist on your Umbraco Node, but you mean to access the property on your custom model.

Change:

@{Model.Content.GetPropertyValue<string>("MyProperty1");}

To:

@{var myProperty1 = Model.MyProperty1;}

Upvotes: 2

Related Questions