C
C#7mo ago
Ayin

How things get to the place

I have found this one thing by Microsoft that confused me so much
public async Task<IActionResult> OnPostTryValidateAsync()
{
var modifiedReleaseDate = DateTime.Now.Date;
Movie.ReleaseDate = modifiedReleaseDate;

ModelState.ClearValidationState(nameof(Movie));
if (!TryValidateModel(Movie, nameof(Movie)))
{
return Page();
}

_context.Movies.Add(Movie);
await _context.SaveChangesAsync();

return RedirectToPage("./Index");
}
public async Task<IActionResult> OnPostTryValidateAsync()
{
var modifiedReleaseDate = DateTime.Now.Date;
Movie.ReleaseDate = modifiedReleaseDate;

ModelState.ClearValidationState(nameof(Movie));
if (!TryValidateModel(Movie, nameof(Movie)))
{
return Page();
}

_context.Movies.Add(Movie);
await _context.SaveChangesAsync();

return RedirectToPage("./Index");
}
Here, how did we get object of Movie? Method doesn't have any aprameters so I simply cannot get it
6 Replies
Ayin
AyinOP7mo ago
Doesn't it need something like ([FromForm] Movie movie) to get model?
canton7
canton77mo ago
Some context would be good. It's probably a property, but hard to say for sure without more info
Ayin
AyinOP7mo ago
Are controllers supposed to have properties? Other than services and configs
Saber
Saber7mo ago
that specific example would be razor pages so its not a controller
Ayin
AyinOP7mo ago
So I would still need [FromForm] to use ModelState in controller
Want results from more Discord servers?
Add your server