如果 onclick to button 我可以在不重新加载页面和回发的情况下显示 CustomerPartial。如果我点击按钮,我会将数据发布到主页/客户。但是我的浏览器链接(url)总是在下面
本地主机:49461
当我使用 json post to Home/Customer 时,如何在浏览器链接(url)中显示下面的链接(url)?
本地主机:49461/家/客户
我问这个是因为浏览器链接(url)总是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);
}
客户部分:
<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();
}
}