question

JayVijayModi-2717 avatar image
0 Votes"
JayVijayModi-2717 asked Bruce-SqlWork answered

Using Autoquery to get data from an MVC Application

So I have created a basic MVC Application with the CRUD operations using Entity Framework. But I have been told to add AutoQuery in the application so as to when the user says /customer?select=customerId,name,color,customerCategory.name,salesOrderDetail,salesOrderDetail.customer then it should return a JSON format of the data that are selected. I am confused as to from where to start if I'm using autoquery.
I have 5 entities Customer, Team, Session, SessionEventLog and Request Category. I have created a CustomerController for performing customer data CRUD operations but this is not Autoquery as what I'm doing here is this:

 public ActionResult Details(int id)
         {
             using (DatabaseContext DB = new DatabaseContext ())
             {
                 return View(DB.Customers.Where(x=> x.RefNo == id).FirstOrDefault());
             }                    
         }

This is retrieving all the values but what I want is when I put /customer?select=customerId,name,color,customerCategory.name,salesOrderDetail,salesOrderDetail.customer in the url it will only retrieve these particular values.


dotnet-aspnet-mvcdotnet-entity-framework
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered

What you want is a Linq projection using a custom class. The custom class is sent into the view.

https://csharp-station.com/Tutorial/Linq/Lesson02

But a couple of other things here like understanding the MVC models like a viewmodel as opposed to the persistence model.

The custom class for the Linq projection would be the viewmodel.

https://deviq.com/terms/kinds-of-models

https://www.dotnettricks.com/learn/mvc/understanding-viewmodel-in-aspnet-mvc

You map persistence model object over to a viewmodel object and the viewmodel object is mapped to a persistence model object for data persistence with the database.

understand seperation of concerns.

https://en.wikipedia.org/wiki/Separation_of_concerns

https://www.c-sharpcorner.com/UploadFile/56fb14/understanding-separation-of-concern-and-Asp-Net-mvc/

https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/overview/understanding-models-views-and-controllers-cs

As talked about with the subject on Understanding Models, keep the controller thin void of any database logic that should be in a class in the models folder with methods called upon by the controller.

Example of a MVC solution you can review implementing the things discussed. VM = viewmodel and DM = domain model. The example is doing CRUD with the database using EF sitting behind the WebAPI in the DAL.

Just know that the database logic belongs in a class in the Models folder not in the controller keep the controller thin.

https://github.com/darnold924/PublishingCompany

HTH


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered

You can use a dynamic expression tree you build for the select. See this thread

https://stackoverflow.com/questions/16516971/linq-dynamic-select

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.