ASP.NET MVC - 多对多关系在viewmodel中显示数据

时间:2018-03-29 22:45:32

标签: c# asp.net-mvc entity-framework many-to-many asp.net-mvc-viewmodel

我有两个模型ProductCategory之间应该有many-to-many个关系,我想在List Box中显示我的类别,以便客户选择多个类别,但它& #39; s没有显示任何类别项目,我不知道我做错了什么,所以这就是我所做的:

产品型号:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using meShop.Areas.Admin.Models;

namespace meShop.Areas.Admin.Models
{
    public class Product
    {

        public Product()
        {
            this.Categories = new HashSet<Category>();
        }

        public int  Id { get; set; }

        [Required]
        [StringLength(100)]
        [Display(Name = "Name")]
        public string Name { get; set; }

        [Required]
        [Display(Name = "Price")]
        public decimal Price { get; set; }

        [Required]
        [StringLength(100)]
        [Display(Name = "Product Description")]
        public string Description { get; set; }

        [Required]
        [Display(Name = "Status of Product")]
        public bool Status { get; set; }

        public virtual ICollection<Category> Categories { get; set; }
    }
}

类别型号:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using meShop.Areas.Admin.Models;

namespace meShop.Areas.Admin.Models
{
    public class Category
    {
        public Category()
        {
            this.Products = new HashSet<Product>();
        }

        public int Id { get; set; }

        [Required]
        [StringLength(100)]
        [Display(Name ="Category Name")]
        public string Name { get; set; }

        public virtual ICollection<Product> Products { get; set; }
    }

}

ApplicationDbContex和OnModelCreating方法:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {

        public DbSet<Product> Products { get; set; }
        public DbSet<Category> Categories { get; set; }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Product>()
                .HasMany<Category>(s => s.Categories)
                .WithMany(c => c.Products)
                .Map(cs =>
                {
                    cs.MapLeftKey("ProductRefId");
                    cs.MapRightKey("CategoryRefId");
                    cs.ToTable("ProductCategory");
                });
            modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
            modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
            modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
        }
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }
}

这是我的ViewModel:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using meShop.Areas.Admin.Models;

namespace meShop.Areas.Admin.ViewModels
{
    public class ProductFormViewModel
    {
        public Product Product { get; set; }
        public IEnumerable<Category> Categories { get; set; }
    }
}

毕竟这是我的产品控制器,它将Categories表格DB传递给viewModel:

public ActionResult New()
        {
            var categories = _contex.Categories.ToList();
            var viewModel = new ProductFormViewModel
            {
                Categories = categories
            };
            return View(viewModel);
        }

我的观点

@model meShop.Areas.Admin.ViewModels.ProductFormViewModel

@{
    ViewBag.Title = "New";
    Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml";
}

<h>Add New Product</h>

@using (@Html.BeginForm("Create", "Product"))
{
    <div class="right_col" role="main">
        <div class="form-group">
            @Html.LabelFor(m => m.Product.Name)
            @Html.TextBoxFor(m => m.Product.Name, new { @class = "form-control" })
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.Product.Price)
            @Html.TextBoxFor(m => m.Product.Price, new { @class = "form-control" })
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.Product.Description)
            @Html.TextBoxFor(m => m.Product.Description, new { @class = "form-control" })
        </div>
        <div class="form-group">
            @Html.ListBoxFor(m => m.Categories, new SelectList(Model.Categories, "Name", "Id"),
                    new {@class = "form-control" })
        </div>
    </div>

如果我出错了,请纠正我,谢谢

0 个答案:

没有答案