C
C#8mo ago
bcryption

✅ How to clear old ViewData in ASP.NET?

I have login form and HTML.
public class UserController : Controller
{
// GET
[HttpGet]
public IActionResult Login()
{
ViewData["Login"] = "none";
return View();
}

// get login datas
[HttpPost]
public async Task<IActionResult> Login(string username, string password)
{
LoginMatcher lm = new LoginMatcher(username, password);
User? user = lm.Loginner();
if (user != null)
{
Console.Out.WriteLine("giriş başarılı");
return Json(new { status="error",message="acces accept"});
}
else
{
ViewData["Login"] = "wrong_password";
return View();
}

}
}
public class UserController : Controller
{
// GET
[HttpGet]
public IActionResult Login()
{
ViewData["Login"] = "none";
return View();
}

// get login datas
[HttpPost]
public async Task<IActionResult> Login(string username, string password)
{
LoginMatcher lm = new LoginMatcher(username, password);
User? user = lm.Loginner();
if (user != null)
{
Console.Out.WriteLine("giriş başarılı");
return Json(new { status="error",message="acces accept"});
}
else
{
ViewData["Login"] = "wrong_password";
return View();
}

}
}
<!-- form objects -->
<div class="login-form-forms">
<form asp-action="Login">
<div class="login-form-forms-group">
<label class="login-form-forms-label" for="username">Kullanıcı adı veya email gir</label>
<input class="login-form-button" placeholder="Kullanıcı adı veya email" type="text" id="username" name="username" maxlength="255">
</div>
<label class="login-form-forms-label" for="password">Şifreni gir</label>

@if (ViewData["Login"] == "wrong_password")
{

<div class="login-wrong-password">
<span>Kullanıcı adı veya parola hatalı!</span>
</div>

}
<!-- form objects -->
<div class="login-form-forms">
<form asp-action="Login">
<div class="login-form-forms-group">
<label class="login-form-forms-label" for="username">Kullanıcı adı veya email gir</label>
<input class="login-form-button" placeholder="Kullanıcı adı veya email" type="text" id="username" name="username" maxlength="255">
</div>
<label class="login-form-forms-label" for="password">Şifreni gir</label>

@if (ViewData["Login"] == "wrong_password")
{

<div class="login-wrong-password">
<span>Kullanıcı adı veya parola hatalı!</span>
</div>

}
11 Replies
bcryption
bcryption8mo ago
The wrong password message still appears when I refresh the page. What is wrong?
bcryption
bcryption8mo ago
I tried ViewData.clear but it is same
J&P Dev
J&P Dev8mo ago
Let me check again. @bcryption Your code go wrong password when refresh because user == null. Please modify condition command.
bcryption
bcryption8mo ago
Yes, I fixed it, but when I refresh it, it sends the form again. I need it to literally refresh the page not the form.
J&P Dev
J&P Dev8mo ago
You can use default login form.
bcryption
bcryption8mo ago
wdym? it is default form.
J&P Dev
J&P Dev8mo ago
Aha, then it will work well. let me check again.
bcryption
bcryption8mo ago
Dude form is work, I need disable refresh form
bcryption
bcryption8mo ago
No description
J&P Dev
J&P Dev8mo ago
I will check it again after I came back. I have to go out now. Sorry.
Want results from more Discord servers?
Add your server
More Posts