【问题标题】:Return view() not redirect page返回视图()不重定向页面
【发布时间】:2021-10-12 19:35:33
【问题描述】:

我有两种方法。我用 Ajax 调用了 GetApps 方法。此外,Getapps 重定向到 Apps 方法。但是,“return view()”命令不起作用。它不会重定向到页面。我的错在哪里?

    public IActionResult Apps(ApplicationViewModel model)
    {
        var apps = JsonSerializer.Deserialize<List<ApplicationViewModel>>(TempData["Applicaitons"].ToString());

        return View("/Home/Apps", apps);

    }

    [HttpPost]
    public IActionResult GetApps(string customerId)
    {

        ApplicationResponse apps = new ApplicationResponse();

        var result = _dashboardService.GetApp(Guid.Parse(customerId));

        apps.Applications = result.Result.Data.Applications;

        TempData["Applicaitons"] = JsonSerializer.Serialize(apps.Applications);

        return RedirectToAction("Apps", "Home", new { model = apps.Applications });
    }

【问题讨论】:

  • 这是一个 Ajax 调用,因此“视图”将返回到您的回调。您可以发回参数并更新 window.location... 或仅使用 .html(data) 更新当前页面的一部分。 (我在某些情况下使用它:成功:函数(数据){ params = convertJsonToParams(data); url = "?" + params; setLocation(url); "
  • 你能分享你的ajax代码吗?如何在 ajax 中渲染视图?我建议你也可以使用 F12 网络工具来查看 ajax 发生时发生了什么。控制器是否向客户端返回正确的 html 代码。

标签: asp.net asp.net-mvc asp.net-core


【解决方案1】:

在这种情况下,我认为使用 ajax post 会破坏重定向,我不知道怎么做,但我有一个解决方案

你可以用 windows.location.href 代替 ajax 调用来发送参数。

function GetApps() {
    var customerId = localStorage.getItem('CustomerId');
    var url = window.location.href
    url = '/home/apps?customerId=' + customerId;;
    window.location.href = url;
}

您可以使用 actionresult 中的参数进行操作,并返回模型以在其中查看。

public IActionResult Apps(string customerId)
        {
            ApplicationResponse apps = new ApplicationResponse();
            
            var result = _dashboardService.GetApp(Guid.Parse(customerId));
            apps.Applications = result.Result.Data.Applications;

            return View(apps.Applications);
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-24
    • 2014-06-28
    • 2017-09-15
    • 1970-01-01
    • 2012-12-13
    • 1970-01-01
    • 2016-07-25
    • 1970-01-01
    相关资源
    最近更新 更多