【问题标题】:How to filter the options of a drop down list using another drop down list asp.net core 3.0如何使用另一个下拉列表 asp.net core 3.0 过滤下拉列表的选项
【发布时间】:2020-10-18 22:29:50
【问题描述】:

我想在一个下拉列表中选择一个产品类别并为其显示子类别。 我的模型: 产品类别

        public class ProductCategory
            {
                [Key]
                public int Id { get; set; }
        
                [Required(ErrorMessage ="Pole 'Nazwa kategorii' jest wymagane")]
                [Display(Name ="Nazwa kategorii")]
                public string Name { get; set; }
        
                [Display(Name = "Dodaj zdjęcie")]
                public string ImagePath { get; set; }
        
                public virtual ICollection<ProductSubCategory> ProductSubCategory { get; set; }
        
            }

产品子类别

public class ProductSubCategory
    {
        [Key]
        public int Id { get; set; }
    
        [Required(ErrorMessage = "Pole 'Nazwa podkategorii' jest wymagane")]
        [Display(Name = "Nazwa kategorii")]
        public string Name { get; set; }
    
        [Display(Name = "Wybierz zdjęcie")]
        public string ImagePath { get; set; }
    
        public int ProductCategoryId { get; set; }
    
        [ForeignKey("ProductCategoryId")]
        [Display(Name = "Kategoria")]
        public ProductCategory ProductCategory { get; set; }
    
        public virtual ICollection<Product> Products { get; set; }
    }

创建产品页面

    public IActionResult OnGet()
        {
            ViewData["ProductCategoryId"] = new SelectList(_context.ProductCategory, "Id", "Name");
            ViewData["ProductSubCategoryId"] = new SelectList(_context.ProductSubCategory, "Id", "Name");
            return Page();
        }
        public JsonResult OnGetSubCategories(int category)
        {
            var subcategories = _context.ProductSubCategory.Where(c => c.ProductCategoryId == category).ToList();
            return new JsonResult(new SelectList(subcategories, "Id", "Name"));
        }

创建产品 html

<form method="post" enctype="multipart/form-data">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Product.Name" class="control-label"></label>
                <input asp-for="Product.Name" class="form-control" />
                <span asp-validation-for="Product.Name" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Product.Description" class="control-label"></label>
                <input asp-for="Product.Description" class="form-control" />
                <span asp-validation-for="Product.Description" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Product.ImagePath" class="control-label"></label>
                <input type="file" asp-for="Product.ImagePath" class="form-control" name="image" onchange="readURL(this);"/>
                <span asp-validation-for="Product.ImagePath" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Product.DateOfNotification" class="control-label"></label>
                <input asp-for="Product.DateOfNotification" class="form-control" />
                <span asp-validation-for="Product.DateOfNotification" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Product.ProductCategoryId" class="control-label"></label>
                <select id="category" asp-for="Product.ProductCategoryId" class ="form-control" asp-items="ViewBag.ProductCategoryId"></select>
            </div>
            <div class="form-group">
                <label asp-for="Product.ProductSubCategoryId" class="control-label"></label>
                <select id="subCategory" asp-for="Product.ProductSubCategoryId" class ="form-control" asp-items="ViewBag.ProductSubCategoryId"></select>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
        script
            <script>
            $("#category").change(function () {
            var category = $("#category").val();
            $.ajax({
                url: "CreteProduct?handler=SubCategories",
                method: "GET",
                data: { category: category },
                success: function (data) {
                    $("#subCategory option").remove();
                    $.each(data, function (index, itemData) {       
                        $("#subCategory").append("<option value='" + itemData.Id + "'>" + itemData.Name + "</option>");
                    });
                }
            })
        });
        </script>

结果:子类别下拉列表未定义。选择类别后,项目数量不错,但显示“未定义”。

【问题讨论】:

  • "下拉列表未定义"究竟是什么意思,在哪里未定义?
  • 值显示为未定义。项数正确
  • 那么数据中的示例对象是什么样的?
  • int ID;字符串名称
  • 这并不能反映您正在解析的 javascript 对象从 undefined 获取。收到的结构明显不同于您的预期

标签: javascript asp.net-core drop-down-menu


【解决方案1】:

您需要使用itemData.value 替换itemData.Id,itemData.text 替换itemData.Name。这是一个演示:

cshtml.cs:

public class CreateProductModel : PageModel
    {
        [BindProperty]
        public Product Product { get; set; }
        public static List<ProductCategory> productCategories = new List<ProductCategory> { new ProductCategory { Id = 1, Name = "pc1" }, new ProductCategory { Id = 2, Name = "pc2" } };
        public static List<ProductSubCategory> productSubCategories = new List<ProductSubCategory> { new ProductSubCategory { Id = 11, Name = "psc11", ProductCategoryId = 1 }, new ProductSubCategory { Id = 12, Name = "psc12", ProductCategoryId = 1 }, new ProductSubCategory { Id = 21, Name = "psc21", ProductCategoryId = 2 }, new ProductSubCategory { Id = 22, Name = "psc22", ProductCategoryId = 2 } };
        public IActionResult OnGet()
        {
            ViewData["ProductCategoryId"] = new SelectList(productCategories, "Id", "Name");
            ViewData["ProductSubCategoryId"] = new SelectList(productSubCategories, "Id", "Name");
            return Page();
        }
        public JsonResult OnGetSubCategories(int category)
        {
            var subcategories = productSubCategories.Where(p => p.ProductCategoryId == category).ToList();
            return new JsonResult(new SelectList(subcategories, "Id", "Name"));
        }

    }

cshtml:

<form method="post" enctype="multipart/form-data">
    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
    <div class="form-group">
        <label asp-for="Product.Name" class="control-label"></label>
        <input asp-for="Product.Name" class="form-control" />
        <span asp-validation-for="Product.Name" class="text-danger"></span>
    </div>
    <div class="form-group">
        <label asp-for="Product.Description" class="control-label"></label>
        <input asp-for="Product.Description" class="form-control" />
        <span asp-validation-for="Product.Description" class="text-danger"></span>
    </div>
    <div class="form-group">
        <label asp-for="Product.ImagePath" class="control-label"></label>
        <input type="file" asp-for="Product.ImagePath" class="form-control" name="image" onchange="readURL(this);" />
        <span asp-validation-for="Product.ImagePath" class="text-danger"></span>
    </div>
    <div class="form-group">
        <label asp-for="Product.DateOfNotification" class="control-label"></label>
        <input asp-for="Product.DateOfNotification" class="form-control" />
        <span asp-validation-for="Product.DateOfNotification" class="text-danger"></span>
    </div>
    <div class="form-group">
        <label asp-for="Product.ProductCategoryId" class="control-label"></label>
        <select id="category" asp-for="Product.ProductCategoryId" class="form-control" asp-items="ViewBag.ProductCategoryId"></select>
    </div>
    <div class="form-group">
        <label asp-for="Product.ProductSubCategoryId" class="control-label"></label>
        <select id="subCategory" asp-for="Product.ProductSubCategoryId" class="form-control" asp-items="ViewBag.ProductSubCategoryId"></select>
    </div>
    <div class="form-group">
        <input type="submit" value="Create" class="btn btn-primary" />
    </div>
</form>
@section scripts{ 
    <script type="text/javascript">
            $("#category").change(function () {
                var category = $("#category").val();
                $.ajax({
                url: "CreateProduct?handler=SubCategories",
                    method: "GET",
                    data: {category: category },
                    success: function (data) {
                $("#subCategory option").remove();
                        $.each(data, function (index, itemData) {
                $("#subCategory").append("<option value='" + itemData.value + "'>" + itemData.text + "</option>");
                        });
                    }
                })
            });
</script>
}

结果:

【讨论】:

  • 控制台报错:CreateProduct?handler=SubCategories&category=2:1 加载资源失败:服务器响应状态为 404 ()
  • 你可以在处理程序中看到OnGetSubCategories,类别的类型是int,我更新了我的答案以添加请求url。在你的url类别中是2:1而不是@987654333 @类型。
  • 如何改变这个值
  • 你设置的值在ajax中data: {category: category },我传递了数据'1'这样category=1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-28
  • 2014-09-02
  • 2018-04-16
  • 2019-09-12
  • 1970-01-01
相关资源
最近更新 更多