C
C#9mo 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
AyinOP9mo ago
Doesn't it need something like ([FromForm] Movie movie) to get model?
canton7
canton79mo ago
Some context would be good. It's probably a property, but hard to say for sure without more info
Ayin
AyinOP9mo ago
Are controllers supposed to have properties? Other than services and configs
Saber
Saber9mo ago
that specific example would be razor pages so its not a controller
Ayin
AyinOP9mo ago
So I would still need [FromForm] to use ModelState in controller

Did you find this page helpful?