Copied to clipboard

Flag this post as spam?

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


  • Roger Dunbar 7 posts 77 karma points
    Dec 02, 2015 @ 19:45
    Roger Dunbar
    0

    Passing updated view model to partial view after post (Best practice)

    Hi Guys,

    I imagine that this is something that has been asked before but I've searched and searched and searched and cannot seem to find a definitive answer - so please forgive my ignorance.

    Imagine a scenario where I have a master template, inheriting from the Umbraco Template Page class. This page has a number of partial views that are strongly typed (using view models) and contain forms that post to a Surface Controller.

    So we click on submit on one of the forms in and this submits to the Surface Controller action which does some work, could be many different things, and then we reload the current Umbraco page.

    Ideally at this point we want to be able for the above controller action to populate a view model and find a way of sending this back to the partial view that is being called from the master template/view.

    This appears to be my dilemma. At present, and I'm less than happy about it, I create the view model and assign it to TempData, when the master template/view (it is likely to be a nested template) is being executed it identifies if there is a view model in TempData and uses this, alternatively it generates a new one to pass in as the model to the Partial View.

    Phew! I hope this makes sense because it's confusing the hell out of me.

    So...assuming that this is clearly NOT best practice, how do other developers in the community handle this scenario, i.e. finding a way to pass back strongly typed model data to a partial view that could potentially be a few nested levels down?

    Or am I clearly thinking like a Web Forms developer? Part of me thinks I could do more in the view code itself, but I hate that, so I'm sure I'm missing a trick.

    Thanks,

    Rog

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Dec 11, 2015 @ 00:28
    Marc Goodson
    0

    Hi Rog

    It depends on how complicated your forms are; and to what level of validation they require; using Surface Controllers there isn't a way other than how you describe above to send an updated model back to the same partial view;

    In a simple scenario it's possible to use two Surface controller actions in tandem, one to render the form and another child action to read posted values from forms/querystring collections, and return a strongly typed partial view containing updated model.

    For more complicated scenarios then you can use a technique called 'route hijacking' there are a few more steps to implement, but you have much more control over the model you return to the view.

    https://our.umbraco.org/documentation/reference/routing/custom-controllers

    and finally if your interaction is more like an MVC application alongside Umbraco; you can create an UmbracoVirtualNodeRouteHandler to map a custom UmbracoRoute; to your MVC controllers: more details here:

    http://shazwazza.com/post/custom-mvc-routes-within-the-umbraco-pipeline/

    if that helps ?

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft