【问题标题】:How to use a html select in MVC .net using Automapper? [duplicate]如何使用 Automapper 在 MVC .net 中使用 html 选择? [复制]
【发布时间】:2016-05-10 00:48:46
【问题描述】:

客户端类:

public partial class Client
        {

            public Client()
            {
                this.Produits = new List<Produit>();
            }
            public int idClient { get; set; }
            public string nom { get; set; }
            public string prenom { get; set; }
            public string Email { get; set; }
            public int Tel { get; set; }

            public virtual ICollection<Produit> Produits { get; set; }
        }

产品类别:

public partial class Produit
    {


        public int ProduitID { get; set; }
        public string Type { get; set; }
        public string Description { get; set; }
        public int Prix { get; set; }


        public Nullable<int> client_id { get; set; }
        public virtual Client client { get; set; }
    }

查看产品:

// POST: /Produit/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ProduitID,Type,Description,Prix")] ProduitDTO produit)
        {

            if (ModelState.IsValid)
            {
                _ProduitService.Add(produit);
                return RedirectToAction("Index");
            }

            return View(produit);
        }

我想使用 html &lt;select&gt; 在视图中显示 client_id!
类之间的关系:一个客户有很多产品 我想显示 ProduitID、Type、Description、Prix 和 client_id

public class ClientDTO
    {
        [Key]
        [ScaffoldColumn(false)]
        public int idClient { get; set; }

        [Required(ErrorMessage = "champ requis")]
        [MinLength(3, ErrorMessage = "valeur trop courte")]
        [DisplayName("Nom")]
        public string nom { get; set; }

        [Required(ErrorMessage = "champ requis")]
        [MinLength(3, ErrorMessage = "valeur trop courte")]
        [DisplayName("Prénom")]
        public string prenom { get; set; }

        [Required(ErrorMessage = "champ requis")]
        [MinLength(3, ErrorMessage = "valeur trop courte")]
        [DisplayName("Email")]
        public string Email { get; set; }
        public int Tel { get; set; }
    }                

public class ProduitDTO
    {
        [Key]
        [ScaffoldColumn(false)]
        public int ProduitID { get; set; }

        [Required(ErrorMessage = "champ requis")]
        [MinLength(3, ErrorMessage = "valeur trop courte")]
        [DisplayName("Type")]
        public string Type { get; set; }

        [Required(ErrorMessage = "champ requis")]
        [MinLength(3, ErrorMessage = "valeur trop courte")]
        [DisplayName("Description")]
        public string Description { get; set; }

        [Required(ErrorMessage = "champ requis")]
        [MinLength(3, ErrorMessage = "valeur trop courte")]
        [DisplayName("Prix")]
        public int Prix { get; set; }


        public Nullable<int> client_id { get; set; }
        public virtual ClientDTO client { get; set; }
    }

【问题讨论】:

  • 您是要显示一个选择列表(下拉列表)还是与客户关联的所有产品的表格?
  • 是的,一个下拉列表:)
  • 您使用的是 Razor 视图还是 ASPX 页面?

标签: c# .net model-view-controller


【解决方案1】:

您需要将您的ICollection&lt;Produit&gt; Produits 转换为IEnumerable&lt;SelectListItem&gt;。一个很好的方法是使用一个简单的 lambda:

IEnumerable&lt;SelectListItem&gt; dropdownItems = db.Client.Find(myClientId).Produits.Select(i =&gt; new SelectListItem{ Value = i.ProduitId.ToString(), Text = Produit.Description });

以某种方式将其附加到您的模型:

Model.DropDownItems = dropdownItems;

然后你可以使用 Razor 生成一个下拉列表

@Html.DropDownListFor(Model.DropDownItems)

瞧。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-18
    • 2013-03-13
    • 2014-10-23
    • 2021-08-13
    • 2018-02-09
    • 2014-05-21
    • 2017-08-01
    • 2020-11-23
    相关资源
    最近更新 更多