javascript - 在 asp.net mvc 中使用 ajax post 将数据从 JS 传输到 Controller 中的函数。但 View 没有改变

标签 javascript ajax asp.net-mvc

这是用于传输数据的 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/

相关文章:

c# - ASP.NET - 模型对象列表的 API Url

javascript - 正则表达式查找字符串中的十进制数字

javascript - 计算中间的天数,然后使用嵌套 div 向那些天添加类?

javascript - 如何在 Struts 2 中返回 JSON 数组

javascript - Rails-jQuery。提交后如何重置表格?

asp.net - 使用 Visual Studio 2017 在 Azure 应用服务中发布 ASP.NET 应用程序后出现 HTTP ERROR 404

asp.net-mvc - Asp.Net MVC5,构建大型项目。地区?

asp.net-mvc - MVC 多表一模型

javascript - Highchart 我怎么能给这样的框架?

javascript - Route.get() 需要回调函数,但修改服务器路由后得到一个 [object Undefined]