首页 > 解决方案 > 返回视图()不重定向页面

问题描述

我有两种方法。我用 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 });
    }

标签: asp.netasp.net-mvcasp.net-core

解决方案


在这种情况下,我认为使用 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);
        }

推荐阅读