Cześć,
Robię standardową walidację formularza ale nie chce mi działać atrybut Required
Model:
[Required]
[Display(Name = "Tytuł")]
public string BookName { get; set; }
[Required]
[Display(Name = "Numer ISBN")]
public string ISBN { get; set; }
[Required]
[Display(Name = "Autor")]
public string Author { get; set; }
Widok:
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
@Html.ValidationSummary(false)
<div class="form-group">
@Html.LabelFor(m => m.BookName, new { @class = "control-label" })
@Html.TextBoxFor(m => m.BookName, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.BookName)
</div>
<div class="form-group">
@Html.LabelFor(m => m.Author, new { @class = "control-label" })
@Html.TextBoxFor(m => m.Author, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.ISBN, new { @class = "control-label" })
@Html.TextBoxFor(m => m.ISBN, new { @class = "form-control" })
</div>
<input id="btnSubmit" type="submit" class="btn btn-primary" />
}
Kontroler:
[HttpPost]
public ActionResult Index(Library library)
{
if (this.ModelState.IsValid)
{
var libraryModel = new Database.Model.Library();
libraryModel.Author = library.Author;
libraryModel.BookName = library.BookName;
libraryModel.ISBN = library.ISBN;
bool isCreated = LibraryService.AddBook(libraryModel);
if (isCreated)
{
TempData["success"] = $"Zapisano \"{library.BookName}\"";
}
else
{
TempData["error"] = $"Wystąpił błąd podczas zapisu \"{library.BookName}\"";
}
return RedirectToAction("Index");
}
else
{
return View(library);
}
}
Walidacja pokazuje error message tylko dla pierwszej pozycji oraz żadne pole nie jest oznaczone gwiazdką.