简体   繁体   English

有没有什么简单的方法可以将复杂对象从一个 PageModel 文件传递​​到 asp.net 核心剃刀页面(mvvm,而不是 mvc)中的另一个?

[英]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.

相关问题 如何使用剃刀页面在 ASP.Net Core 上将对象从一个页面传递到另一个页面? - How pass objects from one page to another on ASP.Net Core with razor pages? 简单的注入器注入PageModel ASP.NET核心Razor页面 - Simple Injector Inject into PageModel ASP.NET Core Razor Pages 有没有办法将数据从一个 controller 传递到 ASP.NET MVC 中的另一个? - Is there any way to pass the data from one controller to another in ASP.NET MVC? C# ASP.NET 核心 Razor 页面 - 从 Razor 页面内部调用 PageModel 方法? - C# ASP.NET Core Razor page - call PageModel method from inside the Razor page? 如何在 asp.net 内核 razor 页面 MVVM 之间传递或引用 model - how to pass or reference model between asp.net core razor pages MVVM Net Core 2.0 将数据从 IPageFilter 属性传递到 Razor Pages PageModel - Net Core 2.0 pass data from IPageFilter Attribute to Razor Pages PageModel ASP.NET Razor - 在一个 PageModel 中绑定多个属性和表单 - ASP.NET Razor - Binding multiple properties and forms in one PageModel 如何从 .net 核心中的 pagemodel cs 文件将信息传递回 razor 页面 - how to pass information back to a razor page from the pagemodel cs file in .net core Asp.Net Core Razor 页面模型上具有授权属性的策略的页面授权处理程序在执行设置为文件夹的策略后仍然运行 - Asp.Net Core Razor Pages authorization handlers for policies with Authorize attribute on PageModel still run after executing policies set to folders ASP.NET 核心 MVC 视图中的 Razor 页面路由问题 - Razor Pages routing issue in ASP.NET Core MVC view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM