如果点击按钮我可以显示CustomerPartial而无需重新加载页面和回发。 如果我点击按钮,我会将数据发布到Home / Customer.However我的浏览器链接(网址)总是低于
本地主机:49461
当我使用json帖子到Home / Customer时,如何在浏览器链接(url)中显示以下链接(url)?
本地主机:49461 /家庭/客户
我问这个是因为浏览器链接(网址)总是 localhost:49461
我想看 localhost:49461 / Home / Customer
如何使用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 :(得分:1)
在ajax语句中的.success函数中调用此函数(仅限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);
}
答案 1 :(得分:0)
解决方案:
İ将以下代码添加到我的成功部分javascript代码中。
history.pushState('', 'New URL: '+href, href);