Razor pages EF Core Tutorial

Scott Spencer 26 Reputation points
2021-03-15T15:13:43.587+00:00

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://learn.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.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,157 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Wang-MSFT 1,051 Reputation points
    2021-03-16T07:00:03.443+00:00

    Hi, @Scott Spencer

    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

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Scott Spencer 26 Reputation points
    2021-03-16T11:38:45.397+00:00

    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 person found this answer helpful.