dotnet / AspNetCore.Docs

Documentation for ASP.NET Core
https://docs.microsoft.com/aspnet/core
Creative Commons Attribution 4.0 International
12.58k stars 25.29k forks source link

Why when scaffolding the Movies, does Delete.cshtml.cs have a Movie Property declared with the [BindProperty] attribute #28458

Open YSternlicht opened 1 year ago

YSternlicht commented 1 year ago

Since the Delete.cshtml.cs has a post method which accepts the id (of the movie) can you explain why the Movie property is decorated with the [BindProperty] attribute?

Another (different yet related) point is, why in the OnPostAsync does it assign movie to Movie and then delete Movie. Why not leave out the assignment and remove movie directly?


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

wadepickett commented 1 year ago

Hi @YSternlicht.

The [BindProperty] is introduced and explained not in this model tutorial, but in the scaffolding tutorial.

"The Movie property uses the [BindProperty] attribute to opt-in to model binding. When the Create form posts the form values, the ASP.NET Core runtime binds the posted values to the Movie model."

I will have to look into the 2nd item as I have time, which is also introduced and explained in the scaffolding tutorial, not this model tutorial this issue is on.

YSternlicht commented 1 year ago

I understand that. But I particularly asked why it was used on the Delete.cshtml.cs model where there, it is not needed.