Copied to clipboard

Flag this post as spam?

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


  • Jason Berkan 7 posts 58 karma points
    Jun 05, 2013 @ 19:03
    Jason Berkan
    0

    How do I integrate an MVC form into the Umbraco CMS?

    I've created an MVC form using a PartialView and a SurfaceController using the examples on this website, but I'm having problems figuring out how to integrate that form into the CMS.  Basically, I want my PartialView to replace the page text that is normally defined by my document type.

    I initially thought that a PartialView macro would work, because I could just add it in to the rich text editor, but I have run into problems with it.  Specifically, the only way I can get the macro to work is to put in an @Html.Action(...) pointing to my controller and view, but that causes an unnecessary call to my controller when I do a return CurrentUmbracoPage() from the controller.

    What is the official (or best unofficial) way to integrate a PartialView form into the regular CMS content?

     

  • Charles Afford 1163 posts 1709 karma points
    Jun 06, 2013 @ 21:20
    Charles Afford
    0

    Hi, What is the end result you want to achieve here?

    Surely When your page loads, it loads a view say Forms?

    Then you may have some partial view called RegistrationForm, ContactForm ect ect

    If you use AJAX you can call to the controller and action in your partial view and your action will return a partial view result.

    Thus the result of action will be returned to the page.

    Does this make sense?  Sorry if i have not explained it propertly.  If you need something else, just ask :)

  • Jason Berkan 7 posts 58 karma points
    Jun 07, 2013 @ 16:18
    Jason Berkan
    0

    What I am trying to do is convert a site using Umbraco v4 and MvcBridge to Umbraco v6.  I actually tried to delete this question, as more research proved to me that I cannot easily move my code from MvcBridge to SurfaceControllers, as they don't work the same way.  Primarily, MvcBridge allows me to return any PartialView I want out of a POST, which my code does fairly regularly.  Changing all of that code to work with "return CurrentUmbracoPage()" and putting all of the logic regarding what to display in the View is more work than I am willing to undertake right now.

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Jun 07, 2013 @ 19:00
    Matt Brailsford
    0

    Hi Jason,

    You can use regular controllers with Umbraco aswell as return results other than CurrentUmbracoPage() from your actiona. Surface controllers basically allow you to maintain context within your postback so Umbraco knows what "page" you were on when you submitted the form. If you aren't bothered about context, you can just use a regular controller and return a regular view, or if you just want context within your controller, but want to return a something other than currentumbracopage, you can.

    SurfaceControllers are the simplest way to postback whilst maintainin context, but they certainly aren't the only way. Just another tool in the toolbox.

    Hope that helps.

    Matt

  • Jason Berkan 7 posts 58 karma points
    Jun 07, 2013 @ 19:59
    Jason Berkan
    0

    Matt,

    I'm struggling with the perception that every tool in the toolbox has some issue that makes it not work the same way Umbraco v4 + MvcBridge worked.  In my existing site, my MVC site simply concerns itself with all the fancy form logic inside of PartialViews.  It doesn't know or care about Umbraco at all.  The MvcBridge macro is dropped into a RichTextEditor on my document and pointed at the correct MVC controller and everything works.

    I cannot seem to get the same functionality with SurfaceControllers and PartialView macros, or with hijacking routes, but maybe I'm misunderstanding how things work.

    Currently I've changed over to creating an MVC area that contains my MVC code.  The views in the area call SurfaceControllers defined in Umbraco in order to build the page layout so that it looks the same as if it were inside Umbraco.  It's a bit messy, but at least my MVC code is separated out and does not need to be changed.

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Jun 07, 2013 @ 20:09
    Matt Brailsford
    0

    Hi Jason,

    I must admit, I haven't tried dropping a PartialViewMacro in an RTE, I tend to create specific doc types and have the doc types template include the form after the main content via @Html.Partial("FormPartial"). From there I'll just do the usual SurfaceController begin form code and have it postback to my surface controller, and then from there do some action and redirect or return to current page. I've not returned a partial view before, but I have returned JSON with no problem, so would have thought that was somewhat similar.

    I must also admit, I haven't done much with the MvcBridge either, so couldn't really say how well the concept fits with SurfaceControllers and PartialViewMacros.

    Can you explain what the end result should be? Should the form just postback and return to the current page showing a second state for your form partial view? or should it be doing something else?

    Many thanks

    Matt

  • Jason Berkan 7 posts 58 karma points
    Jun 07, 2013 @ 21:21
    Jason Berkan
    0

    Matt,

    That's exactly what I want.  Essentially, I have a bunch of business functionality that must exist within the same website as the main CMS.  To keep things simple, I just drop the MvcBridge macro into the main page text RTE.  The form (which is a PartialView contained within an existing Umbraco page) POSTs to the controller, which can then do multiple things:

    1. Return the PartialView due to validation failing.
    2. Redirect to a different page completely.
    3. Return a completely different PartialView (generally a specific message for when certain validations fail, such as not having the security rights to perform the action).
    #3 does not work, because returning a PartialView from the POST causes the main Umbraco rendering to not occur, so all you get is the PartialView and nothing from the rest of your page.
  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Jun 09, 2013 @ 15:38
    Andy Butland
    0

    I'm not sure if this is ideal in your scenario and expect you've already thought of it... but if you can't get #3 to work you could fall back to a single PartialView but with some logic to display different things depending on what validation errors have occurred. E.g.

    if (ViewData.ModelState["UnauthorisedAccess"] != null)
    {
        // Display custom message
    }
    else
    {
        // Display form
    }
    
  • Matt Jones 30 posts 141 karma points
    Jun 12, 2013 @ 12:08
    Matt Jones
    0

    Hi

    Could you switch to  use an Ajax form? 

     

    then you can target the area of the page you want to update with your error message

     

    @using (Ajax.BeginForm("ActionName", new { controller = "ControllerNameSurface" }, new AjaxOptions { HttpMethod = "POST", LoadingElementId = "loadingElement", UpdateTargetId="elementIdToUpdate", InsertionMode.Replace, OnSuccess = "jsSuccessFunction", }, new { enctype = "multipart/form-data" }))
    {
    @* your form here *@
    }
Please Sign in or register to post replies

Write your reply to:

Draft