0

If onclick to button I can show CustomerPartial without reload page and postback. If I click to button , I post data to Home/Customer.However my Browser Link(url) is always below

localhost:49461

When I use json post to Home/Customer , how can I show below link(url) in browser link(url) ?

localhost:49461/Home/Customer

I am asking this because Browser Link(url) always localhost:49461

I want to see localhost:49461/Home/Customer

How to change/redirect browser Link(url) by using Javascript Json ?

HTML:

<button type="button" onclick="MyFunction">Go To Customer Page</button>

Javascript:

function MyFunction() {
$.ajax({
url: "/Home/Customer",
type: "POST",
dataType: "json",
contentType: 'application/json',
success: function (mydata) {
$("#MyDiv").html(mydata);
},
error: function () {
$("#MyDiv").html("Error");
}
});
return false;
}

Controller:

public ActionResult Customer(MyModel model)
{
var stringView = RenderRazorViewToString("CustomerPartial", model);
return Json(stringView, JsonRequestBehavior.AllowGet);
}

CustomerPartial:

<h1>Welcome To Customer Page</h1>

For Json:

public string RenderRazorViewToString(string viewName, object model)
{
ViewData.Model = model;
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
return sw.GetStringBuilder().ToString();
}
}
Soner Sevinc
  • 400
  • 4
  • 19
  • possible duplicate of [Changing the URL without reloading the page](http://stackoverflow.com/questions/3381280/changing-the-url-without-reloading-the-page) – ritesh Sep 23 '13 at 11:41

2 Answers2

1

Call this function inside your .success function in your ajax statement (html5 only):

function processAjaxData(response, urlPath){
     document.getElementById("content").innerHTML = response.html;
     document.title = response.pageTitle;
     window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"", urlPath);
 }
Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90
0

Solution:

İ added Below Code to my Succes part inside of javascript code.

history.pushState('', 'New URL: '+href, href);
Soner Sevinc
  • 400
  • 4
  • 19