Copied to clipboard

Flag this post as spam?

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


  • Craig O'Mahony 364 posts 918 karma points
    Apr 10, 2015 @ 13:21
    Craig O'Mahony
    0

    Styling lost on HTTPPost

    Hi all,

    I've obviously done something stupid here but I can't work out what it is.....basically I've set up a MVC controller to display a list of content from Umbraco so....

            public ActionResult Index()
            {
    //Stuff happens here and returns a list to the the view
    }

    On the view there is a search box

                @using(Html.BeginUmbracoForm<STEP_Umbraco.Controllers.CourseController>("Find"))
                {
                    <input class="search" type="text" placeholder="Search" />
                    <a href="/courses.html" class="search-icon"></a>
                }

    And this fires the 

     

            [HttpPost]
            public ActionResult Find(string zSearchTerm)
            {
    //Stuff happens here and returns a filtered list to the the view
    }

    Everything is fine until I click on the search button which fires the Find method and returns the filtered list to the view but the only thing that is being rendered is the HTML that's inside the view itself and not the HTML that's it's wrapped in (ie. All the stuff in the template). Can anyone help please?

    Thanks,

    Craig

     

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Apr 10, 2015 @ 13:35
    Ismail Mayat
    0

    Craig,

    We need to see the code for find and what you are actually returning, ideally you need to return partial view e.g

     return PartialView("~/Views/Partials/Account/SignUpForm.cshtml", new SignupModel());

    Regards

    Ismail

  • Craig O'Mahony 364 posts 918 karma points
    Apr 10, 2015 @ 13:51
    Craig O'Mahony
    0

    Cheers Ismail,

    My controller methods are:

            public ActionResult Index()
            {
     
                var coursePage = Umbraco.Content(1091);
                var courses = new List<CourseViewModel>();
                foreach (var child in coursePage.Children)
                {
                    var iImageLink = child.GetPropertyValue("courseImage");
                    //var zImageLink = Umbraco.NiceUrl(iImageLink);
                    var m = Umbraco.Media(iImageLink);
     
                    courses.Add(new CourseViewModel { iNodeID = child.Id, CourseTitle = child.Name, CourseLink = child.Url, CourseImage = m.Url });
                }
                return PartialView("Courses", courses);
            }
     
            [HttpPost]
            public ActionResult Find(string zSearchTerm)
            { 
                var coursePage = Umbraco.Content(1091);
                var courses = new List<CourseViewModel>();
                foreach (var child in coursePage.Children)
                {
                    var iImageLink = child.GetPropertyValue("courseImage");
                    //var zImageLink = Umbraco.NiceUrl(iImageLink);
                    var m = Umbraco.Media(iImageLink);
     
                    courses.Add(new CourseViewModel { iNodeID = child.Id, CourseTitle = child.Name, CourseLink = child.Url, CourseImage = m.Url });
                }
                return PartialView("Courses", courses);
            }

     

    I know that the code is doing exactly the same thing at the moment, I just didn't want to complicate things!

    and the solution structure is 

    If I change return PartialView("Courses", courses); to return PartialView("~views/partials/courses.cshtml", courses);

    I get the error that

    The relative virtual path '~Views/Partials/Courses.cshtml' is not allowed here.

    Thanks for your help by the way!

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Apr 10, 2015 @ 15:35
    Ismail Mayat
    0

    In your razor why is the anchor pointing to page 

    <a href="/courses.html"class="search-icon"></a>

    Also you  do not have a button to submit the form so how is it being submitted? 

    Regards

    ISmail

  • Craig O'Mahony 364 posts 918 karma points
    Apr 10, 2015 @ 15:38
    Craig O'Mahony
    0

    Hi Ismail,

    That anchor link is a leftover from the original HTML I haven't chained up a button yet so the form is currently just being submitted when the Enter key is pressed.

                @using(Html.BeginUmbracoForm<STEP_Umbraco.Controllers.CourseController>("Find"))

                {

                    @Html.TextBox("zSearchTerm", null, new {id="txtSearch", @class="search", placeholder="Search" })

                }

    Thanks

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Apr 10, 2015 @ 16:02
    Ismail Mayat
    0

    Craig,

    Try it with a button does that make a difference?  Also I highly recommend looking at the source code for hybrid framework its a really good guide to working in mvc with umbraco, will save alot of time and hassle.

    Regards

    Ismail

  • Craig O'Mahony 364 posts 918 karma points
    Apr 10, 2015 @ 16:05
    Craig O'Mahony
    0

    Hi Ismail,

    using a button makes no difference, still the HTML contained within the master template isn't rendered.

    I did start to look at the Hybrid Framework but to be honest i got lost in the code. For someone new to MVC I found it really hard to follow.

    Thanks.

  • Alex Skrypnyk 6148 posts 24077 karma points MVP 8x admin c-trib
    Apr 10, 2015 @ 16:56
    Alex Skrypnyk
    0

    Hi Craig,

    Try to change this line:

    return PartialView("Courses", courses);
    

    To:

    return CurrentUmbracoPage();
    

    It looks like you are returning only partial view, not all the page.

    Thanks

  • Craig O'Mahony 364 posts 918 karma points
    Apr 10, 2015 @ 17:04
    Craig O'Mahony
    0

    Hi Alex,

    Thanks for that, that does indeed keep the styling intact but it doesn't return the filtered list.

    Any ideas?

    Thanks,

    Craig

  • Alex Skrypnyk 6148 posts 24077 karma points MVP 8x admin c-trib
    Apr 10, 2015 @ 17:13
    Alex Skrypnyk
    0

    What filtered list do you mean ?

  • Craig O'Mahony 364 posts 918 karma points
    Apr 10, 2015 @ 17:23
    Craig O'Mahony
    0

    When the page initially loads a List<> is populated (from the backend content) and is displayed on the page.

    Theres a search box on the page that the user populates and hits search (firing the HTTPPOST method). When this runs a filtered list (based upon their search) is constructed and displayed on the page (this is when the styling the lost).

    returnPartialView("Courses", courses);

    Returns the filtered list but the styling is lost

    returnCurrentUmbracoPage();

    Keeps the styling but doesn't pass in the filtered list.

    I am stumped and longing for the days of WebForms! Any help would be grand

  • Alex Skrypnyk 6148 posts 24077 karma points MVP 8x admin c-trib
    Apr 10, 2015 @ 17:27
    Alex Skrypnyk
    0

    Craig, try to change calling your view, go to the Courses main layout, and change Html.Partial helper to the Html.RenderAction, for calling 'public ActionResult Index()' action.

    It's hard to move from webForms to mvc ))

    Thanks

  • Craig O'Mahony 364 posts 918 karma points
    Apr 10, 2015 @ 17:31
    Craig O'Mahony
    0

    Sorry mate you've lost me there!

    Are you talking about in the template that, for instance, i can see in Umbraco? If you are then it currently contains:

    @Html.Action("Index", "Course")

    What should I be changing it to?

    Cheers big man

  • Alex Skrypnyk 6148 posts 24077 karma points MVP 8x admin c-trib
    Apr 10, 2015 @ 17:39
    Alex Skrypnyk
    0

    Strange, looks like everything should work fine. Can you debug ?

  • Craig O'Mahony 364 posts 918 karma points
    Apr 10, 2015 @ 17:42
    Craig O'Mahony
    0

    I can debug it yes but there doesn't appear to be anything wrong. Most of the markup is in the Master template (as normal) but it just won't render it off the HTTPPost event.

    Completely clueless

  • Alex Skrypnyk 6148 posts 24077 karma points MVP 8x admin c-trib
    Apr 10, 2015 @ 17:53
    Alex Skrypnyk
    0

    Like !IsPostBack in the webForms )))

  • Craig O'Mahony 364 posts 918 karma points
    Apr 10, 2015 @ 17:55
    Craig O'Mahony
    0

    Sorta! At least the styling stayed intact though!

  • Alex Skrypnyk 6148 posts 24077 karma points MVP 8x admin c-trib
    Apr 11, 2015 @ 18:20
    Alex Skrypnyk
    0

    Did you find solution ?

  • Craig O'Mahony 364 posts 918 karma points
    Apr 11, 2015 @ 20:38
    Craig O'Mahony
    0

    I haven't mate. The code is at work so I'll try it again on monday

  • Yasir Butt 161 posts 371 karma points
    Apr 12, 2015 @ 14:58
    Yasir Butt
    0

    Hi Craig,

    I had have same problem some time ago. you are doing the same thing which i did and getting only partial view without sytling.

    Problem is that - After post you are returning the partial view as mvc is state less so partial view does not know the parent page.

    Solution: Dont return partial view. Return your view page instead of Partial view in your post controller with all the data which needed to your page view.

    This worked for me :)

    Hope this help you

    Yasir

  • Craig O'Mahony 364 posts 918 karma points
    Apr 12, 2015 @ 16:51
    Craig O'Mahony
    0

    Thanks Yasir I'll try this in morning. 

    I'm brand new to MVC what do you mean return view page instead of partial? 

    Thanks again

  • Yasir Butt 161 posts 371 karma points
    Apr 12, 2015 @ 17:25
    Yasir Butt
    0

    View page is page in which you are using your partial view.

    you have to do some thing like this in your controller

    e.g

    return View("Your view page", model which your view page required)

    Yasir

  • Craig O'Mahony 364 posts 918 karma points
    Apr 12, 2015 @ 18:06
    Craig O'Mahony
    0

    Ok matey I'll have a crack at that in the morning 

  • Craig O'Mahony 364 posts 918 karma points
    Apr 13, 2015 @ 11:11
    Craig O'Mahony
    0

    Hi Yasir,

    Try as I might I'm not getting anywhere with this (it could be I've misunderstood though!).

    To clarify everything this is what I've got:

    In the Umbraco back office I've got a template "Course Search" Which gets most of it's styling from the Master Template (header, footer, CSS references, etc).

    In this template I'm calling the index action of the Course Controller

    Which builds a list and returns it to the Courses View

    This View loops through the list that's being passed in and build a block of HTML up which displays all good (including the styling from contained in the Master template), this view also has a Searchbox that the user populates and calls the "Find" action on the Courses Controller which basically builds another list up and returns back to the Courses View.

    And at this point the page builds with the Filtered list of courses but it doesn't have any of the styling contained in the Master template.

    I just can't see what I've done wrong! Or get it to work!

    Any help would be magic.

    Thanks,

    Craig

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Apr 13, 2015 @ 11:24
    Dave Woestenborghs
    0

    Hi Craig,

    Does your controller inherit from rendermvccontroller or surfacecontroller ?

    If it inherits from rendermvccontroller you need to make sure your courses view has a layout set like this :

    @{
        Layout = "Master.cshtml";
    }

    Dave

  • Craig O'Mahony 364 posts 918 karma points
    Apr 13, 2015 @ 11:28
    Craig O'Mahony
    0

    Hiya Dave,

    It's inheriting from the SurfaceController

    Cheers,

    Craig

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Apr 13, 2015 @ 11:33
    Dave Woestenborghs
    100

    Okay. I think you use a regular post for searching. So in that case the behavior is correct. A surface controller action has it's own routing and can be viewed in the browser.

    Here you can find some examples of using surface controllers in combination with post requests

    http://creativewebspecialist.co.uk/2013/07/22/umbraco-mvc-what-on-earth-is-a-surface-controller/

    http://24days.in/umbraco/2012/creating-a-login-form-with-umbraco-mvc-surfacecontroller/

    Dave

  • Craig O'Mahony 364 posts 918 karma points
    Apr 13, 2015 @ 11:43
    Craig O'Mahony
    0

    Cheers Dave,

    I think that I've read those posts over the last couple of days and even used one as starting example. The thing that I'm not understanding is when the page is first viewed the styling is intact but then lost on post. When the HTTPPOST method is in effect just doing what the Index method does but with a different list.

    Other than putting the whole of pages HTML that's in the Master template into the view is there any other way?

    thanks for your time and patience with my ignorance!

    Craig

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Apr 13, 2015 @ 11:50
    Dave Woestenborghs
    0

    Let me try to explain what is happening.

    You have a method called index on a coursecontroller. You call this action from your master template so it is "embedded" in your page.

    But this action has also has it's own route so you can call it through a url like :

    http://yourdomain/umbraco/surface/course/index ;

    Then you will get the result without the styling defined in the master page.

    So when you post your search form it will post to a route (url) like this :

    http://yourdomain/umbraco/surface/course/find

    Because you call a child action no master page will be applied.

    To keep this styled you can use the approach described in the previous links I posted using RedirectToCurrentUmbracoPage

    Or instead of letting your browser do a post you can do it using ajax and just update the part of the website with the result of the ajax action.

    Dave

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Apr 13, 2015 @ 11:53
    Dave Woestenborghs
    0

    Here is a example of using a surface controller with ajax :

    http://mytechworld.azurewebsites.net/2013/11/easy-umbraco-search-with-razor-surfacecontrollers-and-ajax/

    The post is about Umbraco 6, but this will also work in Umbraco 7

    Dave

  • Craig O'Mahony 364 posts 918 karma points
    Apr 13, 2015 @ 12:01
    Craig O'Mahony
    0

    Ah, now that's beginning to make sense! That's brilliant thanks Dave.

    I shall have a looky into the Ajax post method but if you don't mind answering something else for me (I know!) if I use the RedirectToCurrentUmbracoPage() method is it right that I can't pass in the courses List?

    Thanks again (top man)

    Craig

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Apr 13, 2015 @ 12:04
    Dave Woestenborghs
    0

    When using the redirect method you can not pass your model to your view.

    You will need to set it in the tempdata and possibly with a boolean that you have searched as well.

    After that you can check in your view for that boolean and if it's true than get your results from the tempdata instead of your model.

    Dave

  • Craig O'Mahony 364 posts 918 karma points
    Apr 13, 2015 @ 12:06
    Craig O'Mahony
    0

    Brilliant.

    Thanks again Dave. Lots of help to me.

     

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Apr 13, 2015 @ 12:07
    Dave Woestenborghs
    0

    Don't forget to mark this post as a solution, so others can find the solution as well.

Please Sign in or register to post replies

Write your reply to:

Draft