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
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
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
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
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
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.
This renders the ~/Views/Partials/Item.cshtml partial
The Item.cshtml partial is declared like this:
I initially render it from a parent view like this:
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
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
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
is working on a reply...