[英]Is there any easy way to to pass complex objects from one PageModel file to another in asp.net core razor pages (mvvm, not mvc)?
I am familiar with asp.net mvc development.我熟悉asp.net mvc开发。 In that framework passing complex objects (such as a list of objects) was as simple as creating the public static list in one controller:
在该框架中,传递复杂对象(例如对象列表)就像在一个控制器中创建公共静态列表一样简单:
public static List<T_Material> lstProducts = new List<T_Material>()
{
new T_Material {T_MaterialID = 1, Name = "Product1", Description="Description1", Price=20, MonthsOfAccess=20},
new T_Material {T_MaterialID = 2, Name = "Product2", Description="Description2", Price=20, MonthsOfAccess=20}
}
and referencing in another controller with the following code: ControllerName.lstProducts
并使用以下代码在另一个控制器中引用:
ControllerName.lstProducts
I am building an application in asp.net core razor pages (using razor pages with a PageModel code behind file not MVC) and passing a complex object from one PageModel file is not simple at all.我正在 asp.net 核心剃刀页面中构建一个应用程序(使用带有 PageModel 代码隐藏文件而不是 MVC 的剃刀页面)并且从一个 PageModel 文件传递一个复杂的对象一点也不简单。 I feel like the answer to this should be obvious but I am still scratching my head.
我觉得这个问题的答案应该是显而易见的,但我仍然在挠头。
If you want the object passed from controller to view then use viewbag.如果您希望从控制器传递的对象进行查看,请使用 viewbag。
ViewBag.ListProducts = lstProducts;
ViewBag.ListProducts = lstProducts;
Note this Viewbag can only be access in strongly typed view using your PageMdel.请注意,此 Viewbag 只能使用您的 PageMdel 在强类型视图中访问。
If you want this object to be passed from first request to second request, then use tempdata.如果您希望此对象从第一个请求传递到第二个请求,请使用 tempdata。
TempData[“ListProducts”]= lstPrroducts;
TempData[“ListProducts”]= lstProducts;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.