Reputation: 2643
I have a CodesController and the Index action looks like this.
public ActionResult Index(int id, string manufacturerName)
I want the URL to look like http://www.domain.com/codes/[id]/[manufacturerName] but with the code I currently am using it turns out to look like http://www.domain.com/codes/[id]?manufacturerName=[manufacturerName]
<script type="text/javascript">
$(function() {
$('#btnNext').click(function () {
var _id = $('#ddlManufacturers').val();
var _manufacturerName = $('#ddlManufacturers').text();
var url = '@Url.Action("Index", "Codes", new { id = "_id", manufacturerName = "_manufacturerName" })'
.replace('_id', _id).replace('_manufacturerName', _manufacturerName.toLowerCase().trim());
window.location.href = url;
});
});
This is how I am doing it now and not sure how to properly do it so the URL turns out the way I want. The code
Upvotes: 0
Views: 500
Reputation: 133453
As per my understanding you will need to add an route like
routes.MapRoute(
name: "CodesRoute",
url: "{controller}/{action}/{id}/{manufacturerName}",
new { controller = "Codes", action = "Index",
manufacturerName = UrlParameter.Optional
}
);
After that you can use Html.RouteLink
or Url.RouteUrl
As you are using it JavaScript Url.RouteUrl
will be useful
var url = '@Url.RouteUrl("CodesRoute", new { id = "_id", manufacturerName = "_manufacturerName" })'
Upvotes: 1
Reputation: 27022
You would need a new route:
routes.MapRoute(
name: null,
url: "{controller}/{action}/{id}/{manufacturerName}",
defaults: new {
controller = "Home",
action = "Index",
id = UrlParameter.Optional,
manufacturerName = UrlParameter.Optional
}
);
Upvotes: 0