deichor
✅ Blazor Tutorial (Todo List) Doesn't Work
Hello guys, I'm new in Blazor.
This code published by Microsoft. (https://learn.microsoft.com/tr-tr/aspnet/core/blazor/tutorials/build-a-blazor-app?view=aspnetcore-8.0) I click on the button, but nothing is added to the list.
@page "/todo"
<PageTitle>Todo</PageTitle>
<h1>Todo (@todos.Count(todo => !todo.IsDone))</h1>
<ul>
@foreach (var todo in todos)
{
<li>
<input type="checkbox" @bind="todo.IsDone" />
<input @bind="todo.Title" />
</li>
}
</ul>
<input placeholder="Something todo" @bind="newTodo" />
<button @onclick="AddTodo">Add todo</button>
@code {
private List<TodoItem> todos = new();
private string? newTodo;
private void AddTodo()
{
if (!string.IsNullOrWhiteSpace(newTodo))
{
todos.Add(new TodoItem { Title = newTodo });
newTodo = string.Empty;
}
}
}
@page "/todo"
<PageTitle>Todo</PageTitle>
<h1>Todo (@todos.Count(todo => !todo.IsDone))</h1>
<ul>
@foreach (var todo in todos)
{
<li>
<input type="checkbox" @bind="todo.IsDone" />
<input @bind="todo.Title" />
</li>
}
</ul>
<input placeholder="Something todo" @bind="newTodo" />
<button @onclick="AddTodo">Add todo</button>
@code {
private List<TodoItem> todos = new();
private string? newTodo;
private void AddTodo()
{
if (!string.IsNullOrWhiteSpace(newTodo))
{
todos.Add(new TodoItem { Title = newTodo });
newTodo = string.Empty;
}
}
}
4 replies
✅ 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>
}
15 replies