Home > database >  Return view() not redirect page
Return view() not redirect page

Time:10-14

I have 2 methods. I called GetApps method with Ajax. In addition, Getapps redirect to Apps method. But, "return view()" command not working. It doesn't redirect to the page. Where is my fault?

    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 });
    }

CodePudding user response:

In this case, I think using ajax post breaks the redirect, I don't know how but I have a solution for it

You can send parameters with windows.location.href instead of ajax call in that.

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

and you can do your operations with the parameter in your actionresult and return the model to view in that.

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);
        }
  • Related