Свойства бритвенных страниц в форме, равной нулю - PullRequest
0 голосов
/ 10 апреля 2020

Итак, я пытаюсь создать страницу для редактирования (и удаления) категории в интернет-магазине. Я использую следующий код:

@page
@model EShop.Pages.Administration.EditCategoryModel
@{
    ViewData["Title"] = "EditCategory";
    Layout = "~/Pages/Administration/Shared/_AdminLayout.cshtml";
}

<h1>Editace kategorií</h1>
<form class="border bg-light rounded p-3" method="post">
    <label asp-for="CategoryID"></label>
    <select class="form-control mb-2 mr-sm-2" asp-for="CategoryID" asp-items="@Model.Categories">
    </select>
    <br />
    <label asp-for="DisplayName"></label>
    <input asp-for="DisplayName" />
    <label asp-for="IdentifierName"></label>
    <input asp-for="IdentifierName" />
    <br />
    <label asp-for="ParentID"></label>
    <select class="form-control mb-2 mr-sm-2" asp-for="ParentID" asp-items="@Model.Categories">
        <option value="">Žádná</option>
    </select>
    <br />
    <button type="submit" class="btn btn-outline-primary">Potvrdit změny</button>
    <button type="button" class="btn btn-danger" data-toggle="modal" data-target="#deleteModal">
        Smazat
    </button>
</form>
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="deleteModalLabel">Smazat</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                Opravdu chcete smazat tuto kategorii, její podkategorie a všechny produkty?
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Zrušit</button>
                <a asp-page-handler="Delete" asp-route-CategoryID="@Model.CategoryID" class="btn btn-danger text-white">Smazat</a>
            </div>
        </div>
    </div>
</div>

И следующую модель:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using EShop.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace EShop.Pages.Administration
{
    public class EditCategoryModel : PageModel
    {
        public EditCategoryModel(DataProvider dp)
        {
            _dp = dp;
        }

        public DataProvider _dp { get; set; }

        [TempData]
        public string SuccessMessage { get; set; }
        [TempData]
        public string FailureMessage { get; set; }

        [BindProperty(SupportsGet = true)]
        [Required]
        [Display(Name = "Vybraná kategorie")]
        public int CategoryID { get; set; }
        [BindProperty(SupportsGet = true)]
        [Display(Name = "Nadkategorie")]
        public int? ParentID { get; set; }
        [Display(Name = "Jméno k zobrazení")]
        [Required]
        public string DisplayName { get; set; }
        [BindProperty(SupportsGet = true)]
        [Display(Name = "Identifikační jméno")]
        public string IdentifierName { get; set; }
        public List<SelectListItem> Categories { get
            {
                List<SelectListItem> selectList = new List<SelectListItem>();
                foreach (var item in _dp.GetCategories())
                {
                    if (item.IdentifierName != null)
                    {
                        selectList.Add(new SelectListItem(item.IdentifierName, item.ID.ToString()));
                    }
                    else
                    {
                        selectList.Add(new SelectListItem(item.DisplayName, item.ID.ToString()));
                    }
                }
                return selectList;
            } }
        public IActionResult OnGet(int CategoryID)
        {
            var category = _dp.GetCategory(CategoryID);
            if (category != null)
            {
                this.CategoryID = category.ID;
                this.DisplayName = category.DisplayName;
                this.IdentifierName = category.IdentifierName;
                this.ParentID = category.ParentID;
            }
            return Page();
        }
        public IActionResult OnPost()
        {
            if (ModelState.IsValid)
            {
                _dp.EditCategory(CategoryID, DisplayName, IdentifierName, ParentID);
                SuccessMessage = "Editace úspěšná";
                return Page();
            }
            FailureMessage = "Editace selhala";
            return Page();
        }
        public IActionResult OnGetDelete(int CategoryID)
        {
            _dp.DeleteCategory(CategoryID);
            return RedirectToPage("./ManageCategories.cshtml");
        }
    }
}

Проблема заключается в том, что, несмотря на то, что DisplayName требуется и оно заполняется в форме, когда я нажимаю кнопку , это установлено в нуль. Что еще хуже, по какой-то причине он оценивается как допустимое состояние модели. Это делает всю редакцию не работать. Что я тут не так делаю?

...