views:

83

answers:

3

I have the following route:

routes.MapRoute("Archive", "archive/{year}", new
    {
        controller = "Archive",
        action = "Results"
    }
);

I have the route working correctly, but I would like my url to show as follows:

http://mysite.com/archive/2008

when I click on the search button instead of just:

http://mysite.com/archive

I DON'T want to do this by creating an action link to that url, I want the submit button to do it.

EDIT:

More info. On my home page, I have a textbox that, when the submit button is clicked, passes the year value typed in the textbox to the Action method Results in my Archive controller that takes one parameter, year. I am getting the correct value in the method right now, I just want the year displayed in the url as well. If I manually type in the year in the url like above, it works just as I expect as well.

THANKS!

+1  A: 

You need to set the default value for the {year} parameter as 2008 in that case. Thus, the route you need is as follows:

routes.MapRoute("Archive", 
  "archive/{year}", 
  new { controller = "Archive", action = "Results", year = 2008 } );

You can also make this dynamic:

routes.MapRoute("Archive", 
  "archive/{year}", 
  new { controller = "Archive", action = "Results", year = DateTime.Now.Year } );

However, I should note that this is dynamic only at Web Application start time, since the routes are defined at Application_Start, which happens once, and the value of DateTime.Now.Year is cached as the value at that time.

paracycle
providing a default value just ensures a value gets passed to my handling action, it doesn't put anything in as far as my url structure is concerned
poindexter12
I am sorry, you are right. The answer I provide just makes sure that you get a correct default value. I recommend you follow veggerby's response which seems to answer your question.
paracycle
+3  A: 

You should pass the Year parameter to the BeginForm(), like:

<% 
    using (Html.BeginForm("Results", "Archive", new { year = DateTime.Now.Year }))
    {
        // form
    }
%>
veggerby
I need this value to come from the previous forms post action, not from the current year.
poindexter12
DateTime.Now.Year was only used an example you can set it to whatever you need.
veggerby
+1  A: 

You could also have a hidden form element named "year" that gets posted when you submit the form

<%=Html.Hidden("year",2008) %>
Nick
I need the content to be dynamic from the previous page, not hard coded to 2008.
poindexter12