Copied to clipboard

Flag this post as spam?

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


  • Craig 34 posts 268 karma points
    Jul 06, 2018 @ 12:12
    Craig
    0

    Route Hijacking Ajax Form Posting Problem

    I've working on a project where I've implemented Route Hijacking on a Template basis with my own custom ViewModel inheriting from RenderModel.

    The url displayed to the user is /members/admin/{route parameter}/edit which is rendered successfully via a Controller I have put together that inherits from RenderMvcController. The template displayed contains a simple form which posts to a Controller that inherits from SurfaceController.

    If I use a @Html.BeginUmbracoForm form on this template then the post back to the controller is successful however as soon as I change this to a @Ajax.BeginForm the form doesn't appear to submit. When I look at Chrome's console I can see the submit returns a 500 error with a System.NullReferenceException thrown within the ViewModel's default constructor:

    public class EditViewModel : RenderModel
    {
    public EditViewModel() : base(UmbracoContext.Current.PublishedContentRequest.PublishedContent) { }
    public EditViewModel(IPublishedContent content) : base(content) { }
    }
    

    I'm guessing that as this is an AJAX post that the UmbracoContext is lost so my question is how do I get around this? I could go with one View Model for display purposes that inherits from RenderModel and then a separate one to use for the form post that doesn't inherit from RenderModel but that seems like a bit of a hack.

    Any ideas?

  • Craig 34 posts 268 karma points
    Jul 10, 2018 @ 13:30
    Craig
    100

    Okay. Managed to sort this. Too much to post here so I'll blog about it when I have a moment and provide a link to the solution.

  • Tajamal 69 posts 157 karma points
    6 days ago
    Tajamal
    0

    Hi Craig,

    Did you use RenderMvcController to post from your form? Or simply SurfaceController?

    For me SurfaceController works as it should, as route hijacking isn't needed. But that's not the case with RenderMvcController and I am not sure if this is the best solution.

    Did you figure out anything?

Please Sign in or register to post replies

Write your reply to:

Draft