Copied to clipboard

Flag this post as spam?

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


  • Steve 140 posts 321 karma points
    Feb 27, 2018 @ 04:48
    Steve
    0

    Can't render partial from SurfaceController

    Hi,
    I'm working on a SurfaceController that returns a PartialView with non-umbraco model (it's an IEnumberable list).
    The PartialView won't render - it throws an error along the lines of 'Cannot bind source type Umbraco.Web.Models.RenderModel to model type XYZ...'
    Have tried sending the model with the @Html.Partial("view.cshtml", MyModel) but only get what seems like an empty model as a result.
    Browsing directly to the controller sends back the PartialView as expected. I just can't seem to place it in the Umbraco view!
    Any help greatly appreciated.
    Thanks, Steve

  • Kurt Hamilton 2 posts 92 karma points
    Feb 27, 2018 @ 07:12
    Kurt Hamilton
    100

    The way I have this working is for a parent view that calls a surface controller to render a child partial (and then subsequently returning a partial from an ajax post).

    Let's say I have the following controller.

    using System.Web.Mvc;
    using MyWebsite.ViewModels;
    using Umbraco.Web.Mvc;
    
    namespace MyWebsite
    {
        public class ThingController : SurfaceController
        {
            [ChildActionOnly]
            [HttpGet]
            public ActionResult Item(int id)
            {
                ItemViewModel viewModel = new ItemViewModel();
                return PartialView(viewModel);
            }
        }
    }
    

    This renders the ~/Views/Partials/Item.cshtml partial

    The Item.cshtml partial is declared like this:

    @using MyWebsite.ViewModels;
    @model ItemViewModel
    
    @*HTML*@
    

    I initially render it from a parent view like this:

    @Html.Action("Item", "ThingController", new { eventId = Model.Content.Id })
    
  • Steve 140 posts 321 karma points
    Feb 27, 2018 @ 20:44
    Steve
    0

    Thanks Kurt. This was really helpful.
    Complicating things on our side, the action calls an external API which must run async - and seems an Async method cannot be called in a child action.
    To resolve, I made the action Sync and used Task.Run(() =>.... to make the call.
    Tiny detail in your example @Html.Action render code - seems like the controller name should be just "Thing" (w/o Controller)
    In the end I was able to get it all working. Thank you very much for your help!!
    Cheers, Steve

  • Craig Mayers 164 posts 508 karma points
    Feb 27, 2018 @ 17:29
    Craig Mayers
    0

    Hi Steve,

    This is because you custom model doesn't inherit from RenderModel.

    See the following guide to correct the issue:

    Custom controllers (Hijacking Umbraco Routes)

    Thanks

    Craig

  • Steve 140 posts 321 karma points
    Feb 27, 2018 @ 22:22
    Steve
    0

    Hi Craig,
    Thanks for the reply and the link. I couldn't get this particular technique to work for our project but that could be due to my lack of experience with SurfaceControllers. I had good results with another method (above) but will keep this in mind for future projects.
    Best regards, Steve

Please Sign in or register to post replies

Write your reply to:

Draft