【问题标题】:Many to many relationship and drop down list多对多关系和下拉列表
【发布时间】:2018-07-12 20:27:33
【问题描述】:

我有一个下拉列表如下

<div class="form-group">
    @Html.LabelFor(m => m.Categories, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.DropDownListFor(m => m.Categories, new SelectList(Model.CategoriesDropDownList), "Choose a category", new { @class = "form-control" })
    </div>
    @Html.ValidationMessageFor(m => m.Categories)
</div>

但是,当我检查模型状态是否有效时,我收到一个无效的验证错误。

我的产品表单视图模型如下

using LaptopMart.Models;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace LaptopMart.ViewModels
{
public class ProductFormViewModel
{

    public int Id { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    public decimal? Price { get; set; }

    [Required]
    [DisplayName("Stock")]
    public int? StockQuantity { get; set; }

    [Required]
    public string Description { get; set; }

    [Required]
    [Display(Name="Category")]
    public ICollection<Category> Categories { get; set; }


    public string Image { get; set; }

    public IEnumerable<Category> CategoriesDropDownList { get; set; }

}
}

类别和产品之间存在多对多关系。当我想填写产品表格时,我希望从 CategoriesDropDownList 中选择一个类别到类别中。我该怎么做呢?

【问题讨论】:

  • @KrishnrajRana 是的,没错
  • 您不能将下拉列表绑定到复杂对象的集合(Categories 就是这样) - 下拉列表绑定并回发一个简单值
  • 而你的new SelectList(Model.CategoriesDropDownList) 将永远无法工作! - 建议您参考this Q/A 获取生成下拉列表的代码

标签: c# asp.net .net asp.net-mvc-4


【解决方案1】:

您为@Html.DropDownListFor 指定了错误的模型类型。我的意思是Categories 属性的类型不应该是ICollection&lt;Category&gt;。当您使用DropDownListFor 时 - 第一个参数是您提交表单后存储所选值的属性。所以,在你的情况下,它应该是CategoryId,它的类型应该是int。所以修改后你的代码看起来像这样 -

@Html.DropDownListFor(m => m.CategoryId, new SelectList(Model.CategoriesDropDownList, "CategoryId", "CategoryName"), "Choose a category", new { @class = "form-control" })

和你的模型 -

[Required]
[Display(Name="Category")]
public int CategoryId { get; set; }

【讨论】:

  • 我是新的 asp.net。那么,如果一个产品可以属于多个类别呢?
【解决方案2】:

您可以使用DropDownList 进行一对一,也可以使用ListBox 进行一对多。

我不知道您的数据库模型,但首先您需要更改您的 ProductFormViewModel 类。因为public ICollection&lt;Category&gt; Categories { get; set; }public IEnumerable&lt;Category&gt; CategoriesDropDownList { get; set; } 是一样的。

可能是这样的

public class ProductFormViewModel
{

    public int Id { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    public decimal? Price { get; set; }

    [Required]
    [DisplayName("Stock")]
    public int? StockQuantity { get; set; }

    [Required]
    public string Description { get; set; }

    [Required]
    [Display(Name="Category")]
    public ICollection<Category> Categories { get; set; }


    public string Image { get; set; }
    //one to one
    public int CategoryID { get; set; }
    //one to many
    public IEnumerable<int> CategoryIDS { get; set; }

}

分类类如

public class Category{
    public int CategoryId { get; set; }
    public string CategoryName { get; set; }
}

下拉列表示例

@Html.DropDownListFor(m => m.CategoryID, new SelectList(Model.Categories , "CategoryId", "CategoryName"), "Choose a category", new { @class = "form-control" })

列表框示例

@Html.ListBoxFor(model => model.CategoryIDs, new SelectList(Model.Categories , "CategoryId", "CategoryName"), "Choose a category", new { @class = "form-control" })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-16
    • 2020-10-06
    • 2015-04-13
    • 1970-01-01
    • 2013-01-10
    • 2018-11-14
    相关资源
    最近更新 更多