views:

80

answers:

2

Suppose I have the following within a webpage

<% using (Html.BeginForm("ShowData", "Summary")) %>
<% { %>
<div class="dropdown"><%=Html.DropDownList("CourseSelection", Model.CourseList, new { @class = "dropdown",  onchange="this.form.submit();" })%> </div>
<% } %>

When the user makes a selection from the dropdown the form is submitted and I would like it to link to another page with a URL as follows:

http://localhost:1721/Summary

I have the following routes:

    routes.MapRoute(null, "Summary", new { controller = "Summary", action = "ShowData", CourseSelection = (string) null });

    routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Login", action = "Index", id = UrlParameter.Optional });

When a user selects an item in the dropdownlist, the URL returned is:

http://localhost:1721/Summary/ShowData?CourseSelection = UserSelection

Clearly the first route in the list is not being matched.

I don't want the URL to show the action name and parameter. I simply want to show "Summary" which is what I have hard coded in the URL. How do I achieve this?

A: 
Raj Kaimal
@Raj: Sorry for delay in replying. I have been doing more research but I am still struggling to understand how custom routing works in mvc. I have edited my original post. Are you still able to help?
Remnant
+2  A: 

The problem here is that your route has a default value

CourseSelection = (string)null

Which is not part of the route URL (aka "Summary").

There's special logic when generating a URL that any default values for the route, where the parameter is not in the URL, the parameter you specify must match the default values.

So another way to fix this is:

using (Html.BeginForm("ShowData", "Summary", 
  new {CourseSelection = (string)null})) {
  ...
}

However, since you're posting that value to the action, I don't see why you have CourseSelection as a default in your route. You just need it as an action method parameter and it will automatically get bound when it's in the posted form data.

So an alternate solution is to change your route like so:

routes.MapRoute(null, "Summary", 
  new { controller = "Summary", action = "ShowData" });
Haacked