How to create client side URLs using ASP.NET MVC?

One of the great features of ASP.NET MVC is the routing mechanism. I like to generate my URLs and not break anything when I change routes.

However, I am not sure how I can apply this mechanism on the client side.

Imagine a common scenario in which I have two drop-down lists, and the contents of the second list depend on the selected item in the first list. I want to load the elements of the second list asynchronously when the selection in the first list changes.

The URL using the default route might look like this: / Cars / GetModelsForBrand / Honda

Easy enough ...

var url = '/Cars/GetModelsForBrand/' + $("#brands").val();

What if I change the routing and the URL becomes: / Honda / GetModels

I just broke my code in an unobvious way.

Is there a way to generate URLs from the client side?

+3
source share
1 answer

We had a similar scenario and he solved it by creating a link to the action, and then adding our parameters to it. We also had a case when we were not sure of the action and wanted to install it on the client’s time. In this case, we created a link to the index and performed a replacement on the client side.

Having generated the link, I mean using the helper method Html.ActionLink

+2
source

Source: https://habr.com/ru/post/1715585/


All Articles