tags:

views:

40

answers:

2

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
+1  A: 

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.

griegs
+1  A: 

use RedirectToRoute action result
link

tsinik
Thanks for your answer. However, I fail to see where exactly I should use RedirectToRoute. Could you provide an example?
Andrew
@Andrew, you should really start from beginning again here if you don't understand where RedirectToRoute goes.
jfar
@jfar - I understand how to use RedirectToRoute in basic terms. In fact, I have used them before. However, I cannot see in this instance where I would use it. An example would really help me understand where I would use it in this case.
Andrew
Andrew, The asp.net MVC platform allows you to create a redirect action result which is called RedirectToRoute, Think of it as the perralel of ASP.NET Responce.Redirect method.
tsinik