Copied to clipboard

Flag this post as spam?

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


  • Nicholus Ives 8 posts 78 karma points
    Jan 19, 2022 @ 15:53
    Nicholus Ives
    0

    Post Form Model with Route Hijacking V9 - Can't get the model to post

    I'm trying to post form data from a view that is hijacked. The issue that I'm having is that I can't seem to get the model to post back.

    What I would think I should do is something like this...

        [HttpPost]
        [ValidateUmbracoFormRouteString]
        [ValidateAntiForgeryToken]
        public ActionResult Index(LocationViewModel model)
        {
            return View(model);
        }
    

    However that return an error, Cannot bind source content type Umbraco.Cms.Web.Common.PublishedModels.Location to model type Portal_Core.ViewModel.LocationViewModel

    I've tried following the documentation https://our.umbraco.com/Documentation/Reference/Routing/custom-controllers but I can't seem to find any examples of posting the model back to the controller.

    In Umbraco I have a page called 'Location'

    I have 'LocationController : RenderController' to handle the hijack and [HttpGet] to serve the ViewModel.

    The ViewModel is called 'LocationViewModel'. The page uses as master page so the view model implements ' : PublishedContentWrapped'

        public class LocationViewModel : PublishedContentWrapped
    {
    
        public LocationViewModel(IPublishedContent content, IPublishedValueFallback publishedValueFallback) : base(content, publishedValueFallback)
        {
    
        } ...
    

    In the location view I'm using @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<Portal_Core.ViewModel.LocationViewModel>

    In the form I'm using @using (Html.BeginUmbracoForm<Portal_Core.Controllers.LocationFormController>("Index"))

    LocationFormController is a : SurfaceController

    In the LocationFormController post I can see the data by using

    public IActionResult FormPost(IFormCollection collection)
    

    all of the form data is in the collection so I know it is posting but then I've lost the model.

    I can use

    public IActionResult Index(ContentModel<IPublishedContent> model)
    

    to see the Location (the non hijacked model) but I can't seem to get the LocationViewModel (Hijacked Model).

    Unfortunately all of the example that I've found show a parameterless Post method and don't show how to get the hijacked model back.

    Does anyone have any advice or examples on getting that posted model?

    Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft