Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Joshua Walsh 30 posts 151 karma points
    Feb 24, 2015 @ 07:23
    Joshua Walsh
    0

    Issue using own model in Partial View

    Hi,

    I have the following Partial View: (/Views/Partials/Banner.cshtml)

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<MyProject.Models.Banner>
    
    <div class="test">@Model.Title</div>
    

    And this Model: (/Models/Banner.cs)

    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?

    Thanks, Josh

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Feb 24, 2015 @ 10:54
    Dave Woestenborghs
    0

    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

  • Joshua Walsh 30 posts 151 karma points
    Feb 24, 2015 @ 11:26
    Joshua Walsh
    0

    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.

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Feb 24, 2015 @ 11:32
    Dave Woestenborghs
    0

    What you also can do is change your view.

    Instead of this line :

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<MyProject.Models.Banner>

    Have this one :

    @model MyProject.Models.Banner


    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

  • Joshua Walsh 30 posts 151 karma points
    Feb 25, 2015 @ 00:38
    Joshua Walsh
    0

    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.

  • Joshua Walsh 30 posts 151 karma points
    Feb 25, 2015 @ 01:45
    Joshua Walsh
    0

    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.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Feb 25, 2015 @ 12:14
    Jeroen Breuer
    0

    Glad you got it working. In the Hybrid Framework I've got more examples which might help in the future.

    Jeroen

Please Sign in or register to post replies

Write your reply to:

Draft