Atrybut Required

0

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ą.

0

Przepraszam, problem rozwiązałem, od rana siedzę przy kompie i mieni mi się w oczach, zapomniałem dodać wszędzie @Html.ValidationMessageFor(m => m.*)
Natomiast jeśli chodzi o gwiazdkę to jak mogę ją zrobić? Wydaje mi się, że ona z automatu była ustawiana...

0

Moze to, ale ja sie na tym nie znam

<appsettings> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appsettings>

A jak nie to takie coś (ale to jakieś stare tematy)
https://dzone.com/articles/aspnet-mvc-%E2%80%93-how-show-asterisk

1 użytkowników online, w tym zalogowanych: 0, gości: 1