Home > Enterprise >  Transfer parameter from one controller to another in ASP.NET MVC
Transfer parameter from one controller to another in ASP.NET MVC

Time:08-10

I need to transfer one parameter (string) that I create in one controller to another controller. Till today I used global variable and it worked fine, but now a few users use the application, so I can't verify that the parameter was not rewrite by other users. The parameter is unique. I tried to use HttpContext.Session, but cannot be used in another controller (just returns null).

Do you have any idea how to do it?

And also, is it possible to use one instance of HttpContext.Session in different controllers?

Thank you!

CodePudding user response:

You can try to use tempData[""].

And learn the working of it before you implementing into your project. I suggest you to use only if it meets your requirements.

CodePudding user response:

Create global static variable inside controller class.

public class HomeController
{
    public static string object_name { get; set; }
}

Now in another controller use DependencyResolver to initialize HomeController

public class MyController
{
    private readonly HomeController homeController = DependencyResolver.Current.GetService<HomeController>();
    
    homeController.object_name = "" //Set value
    var a = homeController.object_name; //Get value
}
  • Related