namespace MyProject.Models
{
public class Banner
{
public string Title;
public Banner(string title = "")
{
Title = title;
}
}
}
I'm not sure if it's necessary, but I even made a controller: (/Controllers/BannerController.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Umbraco.Web.Mvc;
using MyProject.Models;
namespace MyProject.Controllers
{
public class BannerController : RenderMvcController
{
public ActionResult Banner()
{
var model = new Banner();
return View(model);
}
}
}
I try to include my Partial view in one of my layouts using the following code:
@Html.Partial("Banner", new MyProject.Models.Banner("TestBanner"))
and I get the following error:
The model item passed into the dictionary is of type 'MyProject.Models.Banner', but this dictionary requires a model item of type 'Umbraco.Web.Models.PartialViewMacroModel'.
I had believed that Umbraco.RenderMacro() was used for rendering MacroPartials (which I believe PartialViewMacroModel relates to) and standard Html.Partial() would allow me to render my partial view with a model of my choosing. Could somebody please inform me of what I'm doing wrong?
I don't really understand what actions are for and when I do and don't need one. I originally did not have a Controller class at all and thought adding it may resolve my issues, but adding it resulted in no change whatsoever. I'm not sure what Html.Action() does, but a quick look on MSDN suggests it's not what I'm looking for, as all I wish to do is render a Partial View with my Model. I fear I may have overcomplicated things by introducing the unnecessary Controller.
That said, I'll give Html.Action() a try when I get in to work tomorrow, thanks for suggesting it.
Argh I feel stupid now, one of my team members had already created a Banner.cshtml file in /Views/MacroPartials, so when I tried to render my view in /Views/Partials it was picking the wrong one.
Issue using own model in Partial View
Hi,
I have the following Partial View: (/Views/Partials/Banner.cshtml)
And this Model: (/Models/Banner.cs)
I'm not sure if it's necessary, but I even made a controller: (/Controllers/BannerController.cs)
I try to include my Partial view in one of my layouts using the following code:
and I get the following error:
I had believed that Umbraco.RenderMacro() was used for rendering MacroPartials (which I believe PartialViewMacroModel relates to) and standard Html.Partial() would allow me to render my partial view with a model of my choosing. Could somebody please inform me of what I'm doing wrong?
Thanks, Josh
Why have a action on your controller if you don't use it ?
You can do this in your template @Html.Action("Banner","Banner")
Dave
Hi Dave,
I don't really understand what actions are for and when I do and don't need one. I originally did not have a Controller class at all and thought adding it may resolve my issues, but adding it resulted in no change whatsoever. I'm not sure what Html.Action() does, but a quick look on MSDN suggests it's not what I'm looking for, as all I wish to do is render a Partial View with my Model. I fear I may have overcomplicated things by introducing the unnecessary Controller.
That said, I'll give Html.Action() a try when I get in to work tomorrow, thanks for suggesting it.
What you also can do is change your view.
Instead of this line :
Have this one :
Reading your previous post I think you don't have a lot of experience with ASP.NET MVC. Maybe you have to get familiar with it.
http://www.asp.net/mvc
http://www.asp.net/mvc/videos/pluralsight-building-applications-with-aspnet-mvc-4
Dave
I tried @model MyProject.Models.Banner and I still get the same error message.
I think adding the controller was a red herring, so I've deleted it. Same error persists.
Argh I feel stupid now, one of my team members had already created a Banner.cshtml file in /Views/MacroPartials, so when I tried to render my view in /Views/Partials it was picking the wrong one.
Glad you got it working. In the Hybrid Framework I've got more examples which might help in the future.
Jeroen
is working on a reply...