这是用于传输数据的 JS 文件中的代码。
var requestData = {
lati: latitude,
longi: longitude
};
$.ajax({
url: '/User/populate_place',
type: 'POST',
data: requestData,
dataType: 'json'
});
这是我在 Controller 中的功能。
[HttpPost]
public ActionResult populate_place(string lati, string longi)
{
list_placesModels list_place = new list_placesModels();
list_place.Latitude = lati;
list_place.Longitude = longi;
return RedirectToAction("About", "Home");
}
调试程序时调用函数但 View 未更改。它仍然显示旧 View 。
最佳答案
其中一种简单的方法是,您可以将 URL 交给客户端自行处理,方法如下:
[HttpPost]
public ActionResult MyRedirectAction()
{
ViewBag.Message = "Your contact page.";
return Json(Url.Action("About", "Home")); //You can find how i am returing the url
// for 'About' action of 'HomeController' so that
// it will return the corresponding view.
}
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}
现在在您的 View 脚本(可能是 Jquery)中您可以重定向,如下所示:
<input type="button" id="btnRedirectToAbout" value="Redirect To About" />
<script src="../../Scripts/jquery-1.7.1-vsdoc.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-1.7.1.js" type="text/javascript"></script>
<script type="text/javascript">
$("#btnRedirectToAbout").click(function () {
$.post('@Url.Action("MyRedirectAction","Home")', function (data) {
document.location = data;
});
});
</script>
我希望这可以帮助您解决问题。
关于javascript - 在 asp.net mvc 中使用 ajax post 将数据从 JS 传输到 Controller 中的函数。但 View 没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15267922/