I'm on a project with .Net Core and I'm using ASP Razor Pages. In my model, I have an OnGet which load all the data I need in my view.
public IList<Projet> Projets { get; set; }
public ActionResult OnGet()
{
Projets = _serviceSelect.getProjets();
return Page();
}
Then, in my OnPost which is activated when I submit my form.
<form method="post">
<div asp-validation-summary="All" class="text-danger"></div>
<input type="radio" value="1" asp-for="LoginData.TypeCompte" />choice<br />
Username: <input asp-for="LoginData.Username" /><br />
Password: <input asp-for="LoginData.Password" /><br />
Remember me: <input asp-for="LoginData.RememberMe" type="checkbox" /><br />
<input asp-page-handler="connexion" type="submit" value="Login" />
@Html.AntiForgeryToken()
</form>
I would like to display an error in my view, using my ModelState.
public ActionResult OnPostConnexion()
{
if (ModelState.IsValid)
{
// Do stuff
}
else
{
ModelState.AddModelError("", "username or password is blank");
return Page();
}
}
But, when I return Page(), It's like the model is reload and when I try to access to my data, my objects are null.
Object reference not set to an instance of an object.
@foreach (var item in Model.Projets)
How can I update my view without losing my data contain in the model ?