'HTML code between Razor C# code not refreshing
Prefacing this by saying this is the first time I've ever done any kind of web development.
I am trying to ease the process of making my website by putting everything I need into 1 HTML page that would be updated based on buttons that are pressed or other user states (Logged in or not).
I am currently trying to make my page ask the user to login and, once that is done, the actual content would be displayed. I have a Dictionary with a boolean that I index into using Session.SessionID to verify this for testing, but the problem is that once the login button is pressed, the page fails to refresh with the actual content.
Here's what I tried:
Views/Home/Index.cshtml
@{
bool CurrentSessionLoggedIn=false;
if(SessionList.SessionList.ContainsKey(Session.SessionID)){
if(SessionList.SessionList[Session.SessionID].LoggedIn){
CurrentSessionLoggedIn=true;
}
}
if(CurrentSessionLoggedIn){
<table class="table">
<thead>
<tr class="table-primary">
...
}
else
{
...
<button type="submit"class="btn btn-success"onclick="location.href='@Url.Action("LogIn", "Home")'">Loghează-te</button>
}
Controllers/HomeController.cs
public ActionResult Index(){
return View();
}
public ActionResult LogIn(){
SessionList.SessionList[Session.SessionID]=new Session{LoggedIn=true};
return View("Index");
}
As you can see, I am trying to interleave C# and conditionally display HTML while always returning to the same view.
What actually happens is that the code does run and it does log me in, but the page remains the same and a /? is added at the end of the URL in the browser window.
Any idea if there is any way I could achieve this? It would be preferable to me to be able to have everything in one place.
Thank you!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
