Skip to content

Instantly share code, notes, and snippets.

@mattbrailsford
Last active December 20, 2015 12:59
Show Gist options
  • Save mattbrailsford/6134978 to your computer and use it in GitHub Desktop.
Save mattbrailsford/6134978 to your computer and use it in GitHub Desktop.
public class ListingPageController : RenderMvcController
{
public override ActionResult Index(RenderModel model)
{
// Do stuff here
return CurrentTemplate(new ListingViewModel(model)
{
CurrentListing = listing
});
}
}
@using MyNamespace.Web.ViewModels
@inherits Umbraco.Web.Mvc.UmbracoViewPage<ListingViewModel>
@{
Layout = "Master.cshtml";
}
<h1>@Model.CurrentListing.Name</h1>
// A issue with Umbraco hijacked routes and custom view models
// means our viewmodel currently has to inherit from RenderModel
public class ListingViewModel : RenderModel
{
public ListingViewModel(RenderModel model)
: this(model.Content, model.CurrentCulture)
{ }
public ListingViewModel(IPublishedContent content, CultureInfo culture)
: base(content, culture)
{ }
public ListingViewModel(IPublishedContent content)
: base(content)
{ }
public Listing CurrentListing { get; set; }
}
@sniffdk
Copy link

sniffdk commented Aug 1, 2013

If you return a view like you do in normal Mvc, then it's perfectly fine to return a custom model that doesn't inherit from RenderModel. But then you have to specify the view yourself, which I'm guessing you would like to avoid?

public class ListingViewModel : RenderMvcController
{
    public override ActionResult Index(RenderModel model)
    {
        ...

        return View("SomeListingView", new ListingViewModel());
    }    
}

------------------

@inherits UmbracoViewPage<ListingViewModel>
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>Listing view</h1>

At least, I have that working perfectly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment