Partial Macro View issue (Custom Model and RenderModel)
Hi everyone,
I'm posting this issue here dispite discussing elsewhere as I've moved on and beleive I can explain this simply.
The issue in a nutshell is this. I have created a SurfaceController, Custom Model and Partial View, as per umbraco standard practice.
I have then added this SurfaceController to a MacroPartial with @Html.Action("actionname","surfacecontrollername"). The idea is that this is a re-usable PMV that can be dropped into different RTE's on different pages. I am using strongly typed classes throughout.
This is fine and dandy, in my view I inherit like so
This gives me my custom Model with its custom properties and functions, Plus I get the Umbraco helper so this is great. Here comes the issue though, part of my view returns links to the currentpage with query strings. In order to build the link I need :
@Model.Content.NiceUrl()
As I said I use strongly typed clases, however Model = MyModel which is not IPublishedContent and is not inherited from RenderModel, therefor Model.Content does not exist. So ca nsomeone please explain to me either :
a. How do I expose Model.Content on my current ViewModel?
b. How do I code my ViewModel to inherit from RenderModel AND work in the RTE context?
Any code or extra info readily available! I am experianced with .NET, MVC and OOM development, but am lacking experiance with ASP MVC, Umbraco MVC, Umbracco framework. Therefor any help getting me stepped through would be a total life saver!
I used guides from umbraco.tv to build my initial SurfaceController ect.
I have seen a solution to inherit from RenderModel here and here (and many other places) and this works fine on a Hijacked Page controller, however the UmbracoContext.Current.PublishedContentRequest is null when rendered in the RTE.
I have also read this post but it's a bit above my understanding of Umbraco! FYI I'm using Umbraco 7.1.4.
Sorry, I should have added one other supposition...
c. Have I completely missed the point of this? All I want is to get a reference to the current page in a strongly typed fashion within the view of the PVM. Because the PVM is designed to be put anywhere on the site via RTE or Template, I need to know the current page so I can position properly and create query links (may be more use cases as I go!).
Ok, I have a solution of sorts, and that is to pass the Model.Content along with my call to Html.Action. So we have the Partial View Macro here :
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@Html.Action("Index", "MySurface", new { con = Model.Content, cul = Culture })
This leads to my SurfaceController :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;
using Umbraco.Web.Mvc;
using Umbraco.Web.Models;
using Umbraco.Core.Models;
using MyProject.Models;
namespace MyProject.Controllers
{
public class MySurfaceController : SurfaceController
{
[ChildActionOnly]
public ActionResult Index(IPublishedContent con, CultureInfo cul)
{
return PartialView("_myPartial", new MyPartialViewModel(con, cul));
}
}
}
Which I then pass the Content and Culture to MyPartialViewModel..
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using Umbraco.Core.Models;
using Umbraco.Web;
using Umbraco.Web.Models;
namespace MyProject.Models
{
public class MyPartialViewModel : RenderModel
{
public MyPartialViewModel(IPublishedContent content, CultureInfo culture)
: base(content, culture)
{
//constructor
}
}
}
My Model inherits from (the infamous) RenderModel, so requires an IPublishedContent which we passed all the way from the PartialMacroView instead of relying on UmbracoContext.Current.PublishedContentRequest which can be null. so finally my view :
@inherits Umbraco.Web.Mvc.UmbracoViewPage<MyProject.Models.MyPartialViewModel>
<div><p>Hello Partial View (the rebuild)</p></div>
<p>@Model.Content.NiceUrl()</p>
So my final question...
Is this the correct or advisable way to acheive what I want, or is there some sort of glaring issue or unsafeness about all this??
Partial Macro View issue (Custom Model and RenderModel)
Hi everyone,
I'm posting this issue here dispite discussing elsewhere as I've moved on and beleive I can explain this simply.
The issue in a nutshell is this. I have created a SurfaceController, Custom Model and Partial View, as per umbraco standard practice.
I have then added this SurfaceController to a MacroPartial with @Html.Action("actionname","surfacecontrollername"). The idea is that this is a re-usable PMV that can be dropped into different RTE's on different pages. I am using strongly typed classes throughout.
This is fine and dandy, in my view I inherit like so
This gives me my custom Model with its custom properties and functions, Plus I get the Umbraco helper so this is great. Here comes the issue though, part of my view returns links to the currentpage with query strings. In order to build the link I need :
As I said I use strongly typed clases, however Model = MyModel which is not IPublishedContent and is not inherited from RenderModel, therefor Model.Content does not exist. So ca nsomeone please explain to me either :
a. How do I expose Model.Content on my current ViewModel? b. How do I code my ViewModel to inherit from RenderModel AND work in the RTE context?
Any code or extra info readily available! I am experianced with .NET, MVC and OOM development, but am lacking experiance with ASP MVC, Umbraco MVC, Umbracco framework. Therefor any help getting me stepped through would be a total life saver!
Ref:
As I said, been working on this a while! I discuss it in this thread Custom MVC routing with custom RenderModel and HttpPost which is related, the suggestion to look therough Hybrid Framework has just confused me, I'm not seeing PVM's in Hybrid either however I continue to dig!
MVC Guide from docs (starting point) : http://our.umbraco.org/documentation/reference/Templating/Mvc/custom-controllers
I used guides from umbraco.tv to build my initial SurfaceController ect.
I have seen a solution to inherit from RenderModel here and here (and many other places) and this works fine on a Hijacked Page controller, however the UmbracoContext.Current.PublishedContentRequest is null when rendered in the RTE.
I have also read this post but it's a bit above my understanding of Umbraco! FYI I'm using Umbraco 7.1.4.
Sorry, I should have added one other supposition...
c. Have I completely missed the point of this? All I want is to get a reference to the current page in a strongly typed fashion within the view of the PVM. Because the PVM is designed to be put anywhere on the site via RTE or Template, I need to know the current page so I can position properly and create query links (may be more use cases as I go!).
Ok, I have a solution of sorts, and that is to pass the Model.Content along with my call to Html.Action. So we have the Partial View Macro here :
This leads to my SurfaceController :
Which I then pass the Content and Culture to MyPartialViewModel..
My Model inherits from (the infamous) RenderModel, so requires an IPublishedContent which we passed all the way from the PartialMacroView instead of relying on UmbracoContext.Current.PublishedContentRequest which can be null. so finally my view :
So my final question...
Is this the correct or advisable way to acheive what I want, or is there some sort of glaring issue or unsafeness about all this??
Expert feedback is much appriciated!
Thanks for reading
Danny "Blatant"
is working on a reply...