简体   繁体   中英

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. 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

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. 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.ListProducts = lstProducts;

Note this Viewbag can only be access in strongly typed view using your PageMdel.

If you want this object to be passed from first request to second request, then use tempdata.

TempData[“ListProducts”]= lstPrroducts;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM