Copied to clipboard

Flag this post as spam?

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


  • Mike Beale 38 posts 132 karma points
    Feb 08, 2018 @ 09:48
    Mike Beale
    0

    Return PartialView from SurfaceController ActionResult

    Hi,

    I'm creating a SurfaceController along with some partial views to perform a simple search of some external data.

    The Index ActionResult is fine, returns a partial view with the search form with some info, and is complete with the template, I am returning from the Index result like this:

    return PartialView("StaffDirectoryServiceList", model);
    

    The this partial view I am rendering a form:

    @using (Html.BeginUmbracoForm<Hub2.Controllers.StaffDirectorySurfaceController>("StaffSearch", FormMethod.Post))
    {
        <fieldset>
            @Html.TextBoxFor(m => m.staffQuery)
            <input type="submit" value="* Search *" />
        </fieldset>
    }
    

    In the surface controller I have this, just really simple for now:

    [HttpPost]
    public ActionResult StaffSearch(StaffDirectoryViewModel model, String staffQuery)
    {
        String[] searchWords = staffQuery.Split(' ');
    
        model.staffList = new List<MemberOfStaff>();
    
        return PartialView("StaffDirectoryStaffList", model);
    }
    

    This code is working, but when it returns it is literally just the partial view, it no longer has template for the page.

    How can I get this to return with the template?

    Mike

  • Nigel Wilson 945 posts 2077 karma points
    Feb 11, 2018 @ 16:25
    Nigel Wilson
    0

    Hi Mike

    Are you using AJAX for posting to the controller and trying to get the partial view back, or are you posting directly to the Controller ?

    Nigel

  • John Bergman 483 posts 1132 karma points
    Feb 12, 2018 @ 07:20
    John Bergman
    0

    I could be wrong but BeginUmbracoForm should return a View, CurrentUmbracoPage, or something similar.

    If you want a partial view, try using one of the Ajax.BeginForm() overloads calling into a surfacecontroller.

  • Mike Beale 38 posts 132 karma points
    Feb 12, 2018 @ 08:36
    Mike Beale
    0

    Hi Nigel & John, thanks for your replies.

    I'm not using Ajax here, have done in the past on another site, it's an option that I could use but would prefer not to in this case.

    I did try using return CurrentUmbracoPage in the StaffSearch ActionResult but found that returns the partial view from the Index ActionResult & the Model is not updated.

  • Nigel Wilson 945 posts 2077 karma points
    Feb 12, 2018 @ 16:24
    Nigel Wilson
    0

    Hi Mike

    Your return from a controller has to be a view, and if you are only wanting part of the page to be updated, then you will need to use AJAX.

    So if not using AJAX the only true option is to have a view with a custom view model (including search results) and then if necessary pass part of the view model to the partial.

    Hope this helps

    Nigel

Please Sign in or register to post replies

Write your reply to:

Draft