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 }
[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?
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);
[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);
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.
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.
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
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.
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.
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.
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!
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
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!
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?
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....
On the view there is a search box
And this fires the
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
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
Cheers Ismail,
My controller methods are:
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!
In your razor why is the anchor pointing to page
Also you do not have a button to submit the form so how is it being submitted?
Regards
ISmail
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
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
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.
Hi Craig,
Try to change this line:
To:
It looks like you are returning only partial view, not all the page.
Thanks
Hi Alex,
Thanks for that, that does indeed keep the styling intact but it doesn't return the filtered list.
Any ideas?
Thanks,
Craig
What filtered list do you mean ?
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).
Returns the filtered list but the styling is lost
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
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
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
Strange, looks like everything should work fine. Can you debug ?
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
Like !IsPostBack in the webForms )))
Sorta! At least the styling stayed intact though!
Did you find solution ?
I haven't mate. The code is at work so I'll try it again on monday
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
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
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
Ok matey I'll have a crack at that in the morning
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
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 :
Dave
Hiya Dave,
It's inheriting from the SurfaceController
Cheers,
Craig
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
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
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
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
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
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
Brilliant.
Thanks again Dave. Lots of help to me.
Don't forget to mark this post as a solution, so others can find the solution as well.
is working on a reply...