I have been trying to find a solution to this online such as here and here. But they don't seem to help me as it appears that my GET is not updating my view model on page load, whereas those offer an explanation around the POST behaviour.
How are the original values still in the viewModel when the user has navigated completely off the page. I have also tried clearing the browser cache and the original values are still loading.
I would like a clean load every time the user lands on the page.
I am getting the users first name and last name from the Database in my controller.
My Controller Action:
public class CreateArticleController : Controller
{
private static readonly ApplicationDbContext _context = new();
public IActionResult CreateArticle()
{
ModelState.Clear();
CreateArticlePageLocSourceNames _locSourceCreateArticlePageNameReferenceLibrary = new CreateArticlePageLocSourceNames();
var viewModel = new CreateArticleViewModel
{
PageTabTitle = _locSourceCreateArticlePageNameReferenceLibrary.GetLocSourcePageTabTitleNameReferenceForCreateArticlePage(),
Title = _locSourceCreateArticlePageNameReferenceLibrary.GetLocSourceTitleNameReferenceForCreateArticlePage()
//Other viewModel values are also being populated here
};
var user = User.GetUserId<string>();
var userDetails = _context.Users.Find(user);
if (userDetails.FirstName != null)
{
viewModel.UserFirstName = userDetails.FirstName.ToString();
Debug.WriteLine("************* UserFirstName: " + viewModel.UserFirstName);
} else
{
viewModel.UserFirstName = null;
Debug.WriteLine("************* UserFirstName IS NULL");
}
if (userDetails.LastName != null)
{
viewModel.UserLastName = userDetails.LastName.ToString();
Debug.WriteLine("************* UserLastName: " + viewModel.UserLastName);
}
else
{
viewModel.UserLastName = null;
Debug.WriteLine("************* UserLastName IS NULL");
}
return View(viewModel);
}
}
Then I am checking in the view if the first and last names have values. If they do, I use them and if they do not, I show an error message.
@if (Model.UserFirstName == "" || Model.UserFirstName == null || Model.UserLastName == "" || Model.UserLastName == null)
{
<p class="font-danger" id="CreateArticleCardAuthor">@_loc[Model.AddName]</p>
}
else
{
<p class="font-style-content-small-black" id="CreateArticleCardAuthor">@_loc[Model.Author]: @Model.UserFirstName @Model.UserLastName</p>
}
Expected Behaviour:
- Page loads and user does not have a first name in the database
viewModel.UserFirstName
is null- User sees
<p class="font-danger" id="CreateArticleCardAuthor">@_loc[Model.AddName]</p>
error message - User navigates to a different settings page and adds their first name to the database
- User navigates back to original page
- On page load
viewModel.UserFirstName
is populated from the database <p class="font-style-content-small-black" id="CreateArticleCardAuthor">@_loc[Model.Author]: @Model.UserFirstName @Model.UserLastName</p>
is used- User no longer sees error
Actual Behaviour:
- Page loads and user does not have a first name in the database
viewModel.UserFirstName
is null- User sees
<p class="font-danger" id="CreateArticleCardAuthor">@_loc[Model.AddName]</p>
error message - User navigates to a different settings page and adds their first name to the database
- User navigates back to original page
- On page load
viewModel.UserFirstName
is still null <p class="font-danger" id="CreateArticleCardAuthor">@_loc[Model.AddName]</p>
is still used- User still sees the error