5

I have an Ajax form, something like this:

@using (Ajax.BeginForm("AjaxSerchResult", "Search", new { area = string.Empty }, new AjaxOptions() { HttpMethod = "Get", UpdateTargetId = "Results", LoadingElementId = "Loading" }, new { id = "Search" })
{
//Fields go here
}

The question is: how to update the browser url with params i send using AJAX?

Evgeniy Labunskiy
  • 2,012
  • 3
  • 27
  • 45
  • please, take a look on [this](http://stackoverflow.com/questions/136458/change-the-url-in-the-browser-without-loading-the-new-page-using-javascript) – Sergey Jul 19 '14 at 23:10
  • but how to make it work with mvc ajax control? im not an expert in javascript... – Evgeniy Labunskiy Jul 19 '14 at 23:17
  • take a look on my answer below, anyway you have to use javascript, as only by using javascript you can change url. – Sergey Jul 20 '14 at 00:39

1 Answers1

5

if you want to use Ajax.BeginForm(), you would use "OnSuccess" attribute and benalman's plugin, as without javascript you will not able to change url

demo of url changing (jQuery 1.9 required)

@using(Ajax.BeginForm(
      "AjaxSerchResult",
      "Search",
       new { area = string.Empty },
       new AjaxOptions(){
                         HttpMethod = "Get",
                         UpdateTargetId = "Results",
                         LoadingElementId = "Loading",
                         OnSuccess = "changeUrl(data)"
                        },
       new { id = "Search" }))
       {
          //Fields go here
       }

and javascript:

    <script>
    function changeUrl(data) {
        //if you are using benalman's plugin with jQuery 1.9
        location.hash = "#my_hash";
    }
    </script>

Note: but due to using $.browser (that was already removed from jQuery 1.9) in the benalman's plugin, i would recommend to use window.location.hash = "#my_url"; or window.location.replace("#my_url"); instead of location.hash = "#my_url";

Sergey
  • 471
  • 3
  • 10