Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Ajax.BeginForm - Update URL in Browser after successful Get Request

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?

like image 449
Evgeniy Labunskiy Avatar asked Sep 30 '22 07:09

Evgeniy Labunskiy


1 Answers

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";

like image 55
Sergey Boiko Avatar answered Oct 08 '22 05:10

Sergey Boiko