How To Use RedirectToAction With Parameter

Introduction

RedirectToAction is a method in ASP.NET MVC that sends users to either the action method of a different controller or another action method within the same controller. It is an easy way to move about the server or carry out a specific action. RedirectToAction can also accept parameters, which can be used to pass information between actions. This article will discuss how to use RedirectToAction in ASP.NET MVC with arguments.

Syntax

return RedirectToAction("ActionName", "ControllerName", new { argname = argvalue });

The RedirectToAction function's first parameter is the 's name action method's name that should be used as the target. The second parameter is the controller's name, where the action method is situated. An anonymous object serving as the third parameter holds the information the action method will need to perform its function.

Example

/// <summary>
/// Update project details by id
/// </summary>
/// <param name="obj_project">object of Project</param>
/// <param name="id">Id</param>
/// <returns>redirect to ProjectManagement</returns>
[HttpPost]
public async Task < ActionResult > Update_Project(Project obj_project, int id) {
    string url = configuration.GetValue < string > ("Projectapi:BaseApi");
    var baseurl = ServicesEndPonit.ProjectManagement.updatepeoject(url, id);
    if (PutProject(obj_project, baseurl)) {
        return RedirectToAction("Update_Project", "Projectmanagement", new {
            id = obj_project.Id
        });
    }
    return View();
}
public bool PutProject(object data, string url) {
    var jsonData = JsonConvert.SerializeObject(data);
    using(var content = new StringContent(jsonData, System.Text.Encoding.UTF8, "application/json")) {
        httpClient.BaseAddress = new Uri(url);
        HttpResponseMessage result = httpClient.PutAsync(url, content).Result;
        if (result.IsSuccessStatusCode) return true;
        string returnValue = result.Content.ReadAsStringAsync().Result;
        throw new Exception($ "Failed to POST data: ({result.StatusCode}): {returnValue}");
    }
}

As an illustration, let's say you have a controller called Projectmanagement and an action method called Update_Project that takes parameters, id.

/// <summary>
/// Get project details by id
/// </summary>
/// <param name="id">Id</param>
/// <returns>project_(object of Project )</returns>
[HttpGet]
public async Task < ActionResult > Update_Project(int id) {
    Project project_ = new Project(); {
        string url = configuration.GetValue < string > ("Projectapi:BaseApi");
        var baseurl = ServicesEndPonit.ProjectManagement.GetProjectById(url, id);
        httpClient.BaseAddress = new Uri(baseurl);
        HttpResponseMessage Res = await httpClient.GetAsync(baseurl);
        if (Res.IsSuccessStatusCode) {
            var data = Res.Content.ReadAsStringAsync().Result;
            project_ = JsonConvert.DeserializeObject < Project > (data);
        }
        return View(project_);
    }
}

Return RedirectToAction calls this parameterized function with the id.

Conclusion 

In ASP.NET MVC, the RedirectToAction function is used to switch pages and transfer data between action methods. You may quickly reroute users to the relevant page and pass the information required to carry out the planned action using the correct syntax and parameters.

Follow C# Corner to learn more new and amazing things about  ASP.NET MVC or to explore more technologies. If you have any queries/suggestions on the article, please leave your questions and thoughts in the comment section below.

Thanks for reading, and I hope you like it.