Accessing Page URL from within Partial View in MVC - asp.net-mvc

I have a page which has a partial view included within a div called test. I may request more content to be placed within this div by receiving back a PartialViewResult.
I want to be able to obtain the url of the page (i.e. what is shown in the address bar) from within the PartialView code but when I use Request.Url, it gives me the URL of the PartialView only.
Is what I'm trying to do possible at all?

OK, figured out another way of doing it - now using the HTTP_REFERER Request.ServerVariable to work out what page requested it. Not a disaster if the HTTP_REFERER isn't populated, have got a fallback for that

Related

MVC Partial View - Link to location on page

Let's say I have an Html page with divs.
If I want to link to a specific location on that page, I can create a url like http://www.example.com#mylocation
Now let say I'm calling that page as a partial view instead. Can I still get to those page locations?
#Html.Partial("_MyPartialView")
Yes, you can still get to those page locations.
Rendering a partial doesn't do anything special; the final HTML is the same as if you pasted the partial's contents into the same place. (You can even access ViewBag and it should have the same variables in it from the controller.)
If you view the final page source, you'll still see your HTML anchors. You can manually append #mylocation in your browser URL and see that it still works.

ASP.NET, MVC, C# website pass a value though out the application

This is a bit of a puzzle for me.
I need to capture the URI Query string passed to the Home page.
As a user travels to different pages on the web site, I have a partial view that needs that URI query string.
I need a dynamicly created link in the partial view, that equals the original call to the home page.
Example -
If the user goes to - http://www.mysite.com?mode=Joe , I need the dynamicly created link to equal - Http://www.mysite.com?mode=Joe
If the user goes to - http://www.mysite.com?mode=Tommy , I need the dynamicly created link to equal - Http://www.mysite.com?mode=Tommy
FYI - The partial view is used in the _Layout.cshtml file. the call inside - _Layout.cshtml looks like this -
#Html.Partial("MyPartial")
Thanks!
There are a number of ways to do this, but probably the simplest would be to save it to the session on your home page, and then access that session variable from your partial.
You will need to decide what to do if the session expires.
Another possible way would be to write it to a cookie on the home page request and then access the request cookie in your partial. Again, you'd need to decide on an approach for cookies disabled, or wiped out during browsing.
Alternatively, you could look at something like the approach being used in the link below to set a language across the site. Exact implementation would differ, but concept is the same:
Howto automatically add a specific value from current route to all generated links?

MVC - send an AJAX ContentResult to print

Using MVC2 I have an AJAX form which is posting to a bound model. The ActionResult is returned as Content and is inserted into an element on the page by the magic of AJAX forms. All is well.
Now I need a button that will print the returned Content. I've tried returning the result wrapped in a full HTML doc with javascript along the lines of window.print(), but of course this won't execute and even if it did I don't want the whole window.
What is the best way to have my ContentResult printed instead of updated in an element?
I've used jqPrint for this same scenario. It basically either copies the content into an iframe and calls print() on that.
I tried using jqPrint which works for the most part but it was causing problems in IE, I was printing a model dialog.
I ended up using a print style sheet:
http://www.webcredible.co.uk/user-friendly-resources/css/print-stylesheet.shtml
I basically hided all the content of the page except for my dialog, div or whatever it is you are trying to hide.
This was the solution with the most consistent results across browsers.

rails ajax redirect

Here is my use case
I have a search model with two actions search_set and search_show.
1 - A user loads the home page which contains a search_form, rendered
via a partial (search_form).
2 - User does a search, and the request goes to search_set, the search
is saved and a redirect happens to search_show page which again
renders the search_form with the saved search preferences. This search
form is different than the one if step1, because it's a remote form
being submitted to the same action (search set)
3 - Now the user does another search, and the search form is submitted
via ajax to the search_set action. The search is saved and executed
and now I need to present the result via rjs templates (corresponding
to search_show). I am told that if the request is xhr then I can't
redirect to the search_show action? Is that right? If yes, how do I
handle this?
Here is my controller class
http://pastie.org/993460
Thanks
That's right. Either make the request non-XHR and redirect as normal, or you could try rendering the URL you want to redirect to as text or part of a JSON object which your AJAX request then uses to call document.location.href = [whatever] (but this seems hacky).
Right now what's happening is your XHR request is returning the result of the redirect, and not actually redirecting the page that made the XHR request.

How to implement a search page which shows results on the same page?

I'm using ASP.NET MVC 2 for the first time on a project at work and am feeling like a bit of a noob.
I have a page with a customer search control/partial view. The control is a textbox and a button. You enter a customer id into the textbox and hit search. The page then "refreshes" and shows the customer details on the same page. In other words, the customer details appear below the customer search control.
This is so that if the customer isn't the right one, the user can search again without hitting back in the browser. Or, perhaps they mistyped the customer id and need to try again.
I want the URL to look like this:
/Customer/Search/1
Obviously, this follows the default route in the project.
Now, if I type the URL above directly into my browser, it works fine. However, when I then use the search control on that page to search for say customer 2, the page refreshes with the correct customer details but the URL does not change! It stays as
/Customer/Search/1
When I want it to be
/Customer/Search/2
How can I get it to change to the correct URL?
I am only using the default route in Global.asax.
My Search method looks like this:
<AcceptVerbs(HttpVerbs.Get)> _
Function Search(ByVal id As String) As ActionResult
Dim customer As Customer = New CustomerRepository().GetById(id)
Return View("SearchResult", customer)
End Function
A good place to start might be NerdDinner if you havn't already.
In the mean time though The approach I'd use is to have a page that has my search box on it.
Then I'd have a <div> that I name "SearchResults". This will ultimately hold my results to the search.
I then have a PartialView which takes a model that has all the search results and renders them.
So when I click the button I do a call out to a jQuery action that takes the search parameter, performs the search and then returns my PartialView as rendered HTML.
Back in the client side I take that rendered HTML and replace the contents of my div with the HTML.
The keywords to google, or SO, are RenderPartial. This is back end code to render a partial view and give you html.
Also jQuery postbacks so that you can call an action in your controller.
use RedirectToRoute action result
link

Resources