question

ScottSpencer-5200 avatar image
0 Votes"
ScottSpencer-5200 asked ·

Razor pages EF Core Tutorial

Hi,

Trying to learn .Net 5 Core, Razor pages and EF core. I have a question about the Edit Page in the EF Core tutorial.
(https://docs.microsoft.com/en-us/aspnet/core/data/ef-rp/crud?view=aspnetcore-5.0)

I cannot determine where the value for the “int id” parameter in the OnPostAsync method comes from. It is clearly the student id but I don’t see how it is provided. There is no use of “asp-route-id” in the code.

I have commented out the hidden Student.ID input and edited the url to include no student id information but the id parameter still contains a valid student id – where does it come from?

See images in attached pdf.

Thanks for the help.


[1]: /answers/storage/attachments/77856-2021-03-15-10-35-02.pdf

dotnet-aspnetcore-generaldotnet-aspnetcore-razor
10 |1000 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.

miwan2-msft avatar image
0 Votes"
miwan2-msft answered ·

Hi, @ScottSpencer-5200

From the request url we can see, the id is existed in query string.

78047-image.png

and the value of id is from the @page "{id:int?}".

The hidden ID you commented out is Movie.ID. And you could code as below to test.

         public async Task<IActionResult> OnPostAsync()
         {
             if (!ModelState.IsValid)
             {
                 return Page();
             }
    
             _context.Attach(Movie).State = EntityState.Modified;
    
             try
             {
                 await _context.SaveChangesAsync();
             }
             catch (DbUpdateConcurrencyException)
             {
                 if (!MovieExists(Movie.ID))
                 {
                     return NotFound();
                 }
                 else
                 {
                     throw;
                 }
             }
    
             return RedirectToPage("./Index");
         }

Result as you expect

78142-image.png




If the answer is helpful, please click "Accept Answer" and upvote it.
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,
Michael Wang



image.png (18.0 KiB)
image.png (95.0 KiB)
·
10 |1000 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.

ScottSpencer-5200 avatar image
1 Vote"
ScottSpencer-5200 answered ·

Thanks for your reply,

In the ContosoUniversity example there is no route template “{id:int?}” in use.

I do in fact see that the url still contains the original query info; “Students/Edit?id=4” after the save postback which I assume is routed by default to the parameter (int id in this case) with the same name in the handling method.

This information is still there even if I modify the url in the browsers address bar.

So my assumption is that the original request url is stored on the browser somewhere unseen in a page source inspection. The only encoded bit of info on the page source is the anit-forgery token generated by the form in razor pages and I can’t imagine that that is the source of the id info.

Thanks for the help, I stepped away from coding with C# and web forms around 2010. Retirement and Covid gave me the time to try and acquaint myself with Razor pages which seem the most comfortable transition from web forms

Regards,
Scott

· 1 ·
10 |1000 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.

Following your opinion, I tested Request.Query["id"] in view and it is shown as you said. It will be sent when the form post.
78545-image.png



0 Votes 0 ·
image.png (32.7 KiB)