Put variables in the URL in MVC to use with JS

Asked

Viewed 133 times

2

I have a search page that will fetch the results using ajax. Do the searches you do, the URL is always the same, because the page does not change. Is there any way I can change the URL also when I do the search? Ex: I wanted to have a variable that saves the keyword of the search in the URL and then I can search it in a javascript function. Will it be necessary to change the routing of the controller in question? Work with MVC4 and Razorview, and use JS to implement ajax.

I call this method to search (Material/Index):

public ActionResult Index(Portal.Models.MaterialsAdvancedSearchViewModel advancedFilter, string searchParameter, bool chooseMaterialsSpotlight = false)
    {...}

When searching something it will run this method (Material/Materialpagination):

public ActionResult MaterialPagination(int page, int results, string searchParameter, string order_field,
        string order_by, Portal.Models.MaterialsAdvancedSearchViewModel advancedFilter,
        string companyID, bool chooseMaterialsSpotlight = false)
    {...}

In this second method it fetches all the materials I have from the database that correspond to the filters (the input arguments) and places them in a partial view that is loaded using ajax for the view of the first method. I wanted to somehow put these filters in the URL so I can do a URL-only search. I can’t go directly to page 5 right now, and if I click on a search result it opens a new window, I wish I could open it in the same window and then go back and stay where I was in the search.

  • From what you say, basically just change the action verb to GET. But without too much detail and without a code example it’s hard to help you.

  • @jbueno I tried to pass on the information I found essential, but if you need any more details I will be attentive.

1 answer

0

I was able to solve this situation using the HTML5 History API. For those who do not already know can take a look at the mozzila website here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.