Hi @Ronald Rex ,
I want to run some ad hoc queries in my .Net 5 Razor pages or Components. I was wondering should I put the query logic in my models or somewhere else?
For the Razor pages, it has a backend PageModel class file, for example:
Index.cshtml : contains the html elements
and the Index.cshtml.cs file is the backend PageModel class file, you can run the ad hoc queries in the OnGet, OnPost or other Page handler, then display the result on the page.
in the following code, it will use EF core and DbContext (_context) to do the CURD operation. You can change it to your ad hoc queries.
More detailed information, see Introduction to Razor Pages in ASP.NET Core
For the Razor components, you can run the ad hoc queries in the @code
block and use the component's OnInitialized method or other action method. Refer to ASP.NET Core Razor components, ASP.NET Core Blazor data binding and the ASP.NET Core Blazor event handling
Besides, I suggest you could refer to the official tutorial (note in the sample, it uses the EF core to manage data, it will query or edit data via the _context), then you can clear to know where to run the query statement.
if making linq query calls to a webapi is efficent and I read that I should use methods or functions in my models and use Linq to Entities.
Do you mean after getting data from webapi, you want to use LINQ query to filter data? If that is the case, you can do it, but it is better to directly call the WebAPI and get the required value, in this scenario, there is no need to use the linq filter.
And for the models
, I think it should be the PageModel, right? If that is the case, from the above document, we can see that the page model is used to bind data on the page.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion