User classı :
public class User
{
[Required(ErrorMessage = "Ad alanı boş olamaz.")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Soyad alanı boş olamaz.")]
public string LastName { get; set; }
[Required(ErrorMessage = "E-posta alanı boş olamaz.")]
[EmailAddress(ErrorMessage = "Geçerli bir e-posta adresi girin.")]
public string Email { get; set; }
[Range(18, 99, ErrorMessage = "Yaş aralığı 18 ile 99 arasında olmalıdır.")]
public int Age { get; set; }
}
HomeController tarafı
public IActionResult CreateUser()
{
return View();
}
[HttpPost]
public IActionResult CreateUser(User user)
{
if (!ModelState.IsValid)
{
return View(user);
}
// Doğrulama başarılı, kullanıcıyı kaydet
// ...
return RedirectToAction("Index");
}
View tarafı
@model User
@using (Html.BeginForm("CreateUser", "Home"))
{
<div>
<label for="FirstName">Ad:</label>
@Html.TextBoxFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div>
<label for="LastName">Soyad:</label>
@Html.TextBoxFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
<div>
<label for="Email">E-posta:</label>
@Html.TextBoxFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<div>
<label for="Age">Yaş:</label>
@Html.TextBoxFor(model => model.Age)
@Html.ValidationMessageFor(model => model.Age)
</div>
<div>
<input type="submit" value="Kaydet" />
</div>
}