2013-09-23 34 views
0

如果點擊按鈕,我可以顯示CustomerPartial而無需重新加載頁面和回發。 如果我點擊按鈕,我發佈數據首頁/ Customer.However我的瀏覽器鏈接(URL)總是低於Javascript Json重定向到鏈接

本地主機:49461

當我使用JSON帖子主頁/客戶,怎麼能我在瀏覽器鏈接(url)下面顯示鏈接(url)?

本地主機:49461 /家庭/客戶

我問這是因爲瀏覽器鏈接(URL)總是本地主機:49461

我想看看本地主機:49461 /家庭/客戶

如何更改/重定向瀏覽器使用JavaScript Json鏈接(url)?

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

控制器:

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

CustomerPartial:

<h1>Welcome To Customer Page</h1> 

對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(); 
} 
} 
+0

可能重複(http://stackoverflow.com/questions/3381280/changing-the-url-without-reloading-the-page) – Ritesh

+0

感謝僅限Ritesh .. –

回答

0

解決方案:

我添加下面的代碼到我的更迭部分內的JavaScript代碼。 [更改URL無需重新加載頁面]的

history.pushState('', 'New URL: '+href, href); 
1

打電話給你.success函數內部此功能在你的Ajax聲明(僅限HTML5):

function processAjaxData(response, urlPath){ 
    document.getElementById("content").innerHTML = response.html; 
    document.title = response.pageTitle; 
    window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"", urlPath); 
}