简体   繁体   中英

How to use ViewModel in a Razor Pages web app

I have a Models class ProjectDepo that defines 3 dozen properties. This has created a table in the db with those fields. I used Scaffolding to create the iCRUD Razor pages. 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. I would use a ViewModel choosing only those specific properties and querying to filter my results to base my scaffolding off of.

When I have created my ViewModel class, how do I specify which Model class these properties are from? Where can I add a SQL statement to filter the results?

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.

Thank you for any assistance. chuck

(This is my entire ViewModel class, abbreviated)

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.

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)

Anyways, this question can be closed as I was looking for a way to handle this ViewModel WITHOUT creating a Controller.

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

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