[英]Passing data from one controller to another in Dotnet Core 3.1
I am a newbie to the whole MVC pattern, stuck with the following issue:我是整个 MVC 模式的新手,遇到以下问题:
I have 2 controllers, PayReqController
and PayResController
.我有 2 个控制器,
PayReqController
和PayResController
。 I am trying to access data from one action of PayReqController
to another action of PayResController
.我正在尝试将数据从
PayResController
的一个操作访问到PayReqController
的另一个操作。 I have tried using TempData
:我试过使用
TempData
:
TempData["tsnpac"] = tsnpac;
But it doesn't work because we need to redirect to another controller after setting the TempData
.但它不起作用,因为我们需要在设置 TempData 后重定向到另一个
TempData
。 Also tried using session Session["TSNPAC"] = "TSNPAC";
还尝试使用 session
Session["TSNPAC"] = "TSNPAC";
to access it to another controller, but it is giving the value as null
.将它访问到另一个 controller,但它给出的值是
null
。
My scenario is that in PayReqController
, inside an action I have a value which I need to get that in another controller without redirecting to that controller, because my use case is to redirect to some other URL which I get as a response from other service. My scenario is that in
PayReqController
, inside an action I have a value which I need to get that in another controller without redirecting to that controller, because my use case is to redirect to some other URL which I get as a response from other service.
I am a newbie to the whole MVC pattern, stuck with the following issue:我是整个 MVC 模式的新手,遇到以下问题:
I have 2 controllers, PayReqController and PayResController.我有 2 个控制器,PayReqController 和 PayResController。 I am trying to access data from one action of PayReqController to another action of PayResController, I have tried using TempData
TempData["tsnpac"] = tsnpac
but it doesn't work because we need to redirect to another controller after setting the tempData.我正在尝试从 PayReqController 的一个操作访问 PayResController 的另一个操作的数据,我尝试使用 TempData
TempData["tsnpac"] = tsnpac
但它不起作用,因为我们需要在设置 tempData 后重定向到另一个 controller。 Also tried using session Session["TSNPAC"] = "TSNPAC";
还尝试使用 session
Session["TSNPAC"] = "TSNPAC";
to access it to another controller but it is giving the value as null.将其访问到另一个 controller 但它给出的值是 null。
My Scenario is that in PayReqController, inside an action i have a value which i need to get that in another controller without redirecting to that controller because my use case is to redirect to some other URL which i get as a response from other service. My Scenario is that in PayReqController, inside an action i have a value which i need to get that in another controller without redirecting to that controller because my use case is to redirect to some other URL which i get as a response from other service.
I am a newbie to the whole MVC pattern, stuck with the following issue:我是整个 MVC 模式的新手,遇到以下问题:
I have 2 controllers, PayReqController and PayResController.我有 2 个控制器,PayReqController 和 PayResController。 I am trying to access data from one action of PayReqController to another action of PayResController, I have tried using TempData
TempData["tsnpac"] = tsnpac
but it doesn't work because we need to redirect to another controller after setting the tempData.我正在尝试从 PayReqController 的一个操作访问 PayResController 的另一个操作的数据,我尝试使用 TempData
TempData["tsnpac"] = tsnpac
但它不起作用,因为我们需要在设置 tempData 后重定向到另一个 controller。 Also tried using session Session["TSNPAC"] = "TSNPAC";
还尝试使用 session
Session["TSNPAC"] = "TSNPAC";
to access it to another controller but it is giving the value as null.将其访问到另一个 controller 但它给出的值是 null。
My Scenario is that in PayReqController, inside an action i have a value which i need to get that in another controller without redirecting to that controller because my use case is to redirect to some other URL which i get as a response from other service. My Scenario is that in PayReqController, inside an action i have a value which i need to get that in another controller without redirecting to that controller because my use case is to redirect to some other URL which i get as a response from other service.
I am a newbie to the whole MVC pattern, stuck with the following issue:我是整个 MVC 模式的新手,遇到以下问题:
I have 2 controllers, PayReqController and PayResController.我有 2 个控制器,PayReqController 和 PayResController。 I am trying to access data from one action of PayReqController to another action of PayResController, I have tried using TempData
TempData["tsnpac"] = tsnpac
but it doesn't work because we need to redirect to another controller after setting the tempData.我正在尝试从 PayReqController 的一个操作访问 PayResController 的另一个操作的数据,我尝试使用 TempData
TempData["tsnpac"] = tsnpac
但它不起作用,因为我们需要在设置 tempData 后重定向到另一个 controller。 Also tried using session Session["TSNPAC"] = "TSNPAC";
还尝试使用 session
Session["TSNPAC"] = "TSNPAC";
to access it to another controller but it is giving the value as null.将其访问到另一个 controller 但它给出的值是 null。
My Scenario is that in PayReqController, inside an action i have a value which i need to get that in another controller without redirecting to that controller because my use case is to redirect to some other URL which i get as a response from other service. My Scenario is that in PayReqController, inside an action i have a value which i need to get that in another controller without redirecting to that controller because my use case is to redirect to some other URL which i get as a response from other service.
I tried TempData but different than this approach and it worked.我尝试了 TempData 但与这种方法不同并且它有效。
I serialized my list when storing it in TempData我将列表存储在 TempData 中时对其进行了序列化
AccountController:帐户控制器:
TempData["BiUsers"] = JsonConvert.SerializeObject(users);
(users is an IEnumerable of UserModel) (users 是 UserModel 的 IEnumerable)
And deserialized it in another controller in which I wanted to use that IEnumerable.并在另一个 controller 中反序列化它,我想在其中使用那个 IEnumerable。
HomeController:家庭控制器:
var users = JsonConvert.DeserializeObject<IEnumerable<UserModel>>(TempData["BiUsers"].ToString());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.