简体   繁体   English

如何在Razor Pages Web应用程序中使用ViewModel

[英]How to use ViewModel in a Razor Pages web app

I have a Models class ProjectDepo that defines 3 dozen properties. 我有一个Models类ProjectDepo ,它定义了3个属性。 This has created a table in the db with those fields. 这已经在数据库中使用这些字段创建了一个表。 I used Scaffolding to create the iCRUD Razor pages. 我使用了脚手架来创建iCRUD Razor页面。 All the fields are there correctly and are correctly populating the database table. 所有字段都正确存在,并且正在正确填充数据库表。

I want to create an iCRUD scaffold of only some of those fields/properties and filter the results. 我只想创建一个仅包含某些字段/属性的iCRUD支架,然后过滤结果。 I would use a ViewModel choosing only those specific properties and querying to filter my results to base my scaffolding off of. 我将使用ViewModel仅选择那些特定的属性并进行查询以过滤我的结果,以基于我的脚手架。

When I have created my ViewModel class, how do I specify which Model class these properties are from? 创建ViewModel类后,如何指定这些属性来自哪个Model类? Where can I add a SQL statement to filter the results? 在哪里可以添加SQL语句来过滤结果?

I have searched but have not found the right terms. 我已经搜索过但没有找到正确的术语。 Everything is about binding the ViewModel to the View, I'm trying to get the data to the ViewModel. 一切都与将ViewModel绑定到View有关,我正在尝试将数据获取到ViewModel。

Thank you for any assistance. 感谢您的协助。 chuck 卡盘

(This is my entire ViewModel class, abbreviated) (这是我的整个ViewModel类,简称)

public class RequestReporterViewModel

{
    //All fields are from ProjectDepo class
    public int Id { get; set; }  
    public string OrderingPOCName { get; set; }...
}

if you want to map binded properties fron view to model you can use AutoMapper , AutoMapper is best practice for this state. 如果要将映射的绑定属性映射到模型,则可以使用AutoMapper ,AutoMapper是此状态的最佳实践。

for your example : 以您的示例为例:

Mapper.Map(RequestReporter, RequestReporterViewModel, typeof(RequestReporter), typeof(RequestReporterViewModel));

(First, I received notification that someone did NOT like that I chose NOT to capitalize "sql"; that I used parenthesis, had extra spaces and listed the class in my question. ABSOLUTELY NONE of which made my question easier or more difficult to understand. The "suggested" and "approved" edits didn't help the question AT ALL. They didn't provide a solution, didn't make the question clearer - they ONLY made the question meet the suggesters format.) (//end of rant) (首先,我收到通知,有人不喜欢我选择不大写“ sql”;我使用括号,有多余的空格,并在问题中列出了该类。绝对没有一个问题使我的问题更容易或更难以理解。 。“建议”和“批准”的编辑根本没有帮助问题。它们没有提供解决方案,也没有使问题更清楚-他们仅使问题符合建议者格式。)(// end咆哮)

Anyways, this question can be closed as I was looking for a way to handle this ViewModel WITHOUT creating a Controller. 无论如何,当我在寻找一种无需创建Controller即可处理ViewModel的方法时,可以关闭此问题。

I'll keep searching for an answer using different search criteria elsewhere. 我将继续在其他地方使用不同的搜索条件来寻找答案。

thank you to anyone that took the time to read my question, it is appreciated. 感谢您抽出宝贵时间阅读我的问题,我们对此表示赞赏。

chuck 卡盘

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将带有Razor页面的ASP .NET WEB APP变成API? - How to turn ASP .NET WEB APP with Razor Pages into an API? 如何在 Razor 页面中使用相同的单例实例? - How to use the same singleton instance in Razor pages? 如何在 .netcore 6 razor 页面中使用 FormHelper 和 FluentValidation - How to use FormHelper and FluentValidation in .netcore 6 razor pages AWS无服务器应用程序.Net Core Web App剃刀页面 - AWS Serverless Application .Net Core Web App Razor Pages Get url to web api controller method in the Razor Pages app? - Get url to web api controller method in the Razor Pages app? 如何在 razor 页面应用程序中获取 razor 页面的列表? - How can i can get a list of razor pages in a razor pages app? 如何使用 Razor 页面从 ASP.net 核心简单 web 应用程序中的配置读取 json 对象数组 - How to read array of json objects from configuration in ASP.net Core simple web app using Razor pages 如何将扁平化的ViewModel用于Web API方法 - How to use flattened ViewModel for a Web API method 如何扩展Razor页面 - How to extend Razor Pages 如何根据角色向Razor Pages应用添加身份? - How to add identity based on roles to Razor Pages app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM