如何在数据库中创建和保存多对多数据,我不知道如何更新数据库中选择的多个数据

时间:2020-05-23 10:43:13

标签: .net model-view-controller populate

我想从数据库的下拉列表中保存多个选择的值。所以我在.net core mvc中有多对多关系,但是我不知道如何在数据库中实现创建和保存。 发布模型。如何在数据库中创建和保存多对多数据,我不知道如何更新数据库中选择的多个数据 发布模型

namespace one.Models
{
    public class Post
    {
        public int PostId { get; set; }
        [Required]
        public string Title { get; set; }
        public string Description { get; set; }
        public IEnumerable<PostCategory> PostCategories { get; set; }

        [Display(Name = "PublishedDate")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
        public DateTime PublishedDate { get; set; }
        public Post()
        {
            PublishedDate = DateTime.Now;
        }
    }
}

类别模型

namespace one.Models
{
    public class Category
    {
        public int CategoryId { get; set; }
        public string CategoryName { get; set; }
        public IEnumerable<PostCategory> PostCategories { get; set; }
    }
}

PostCategory模型

namespace one.Models
{
    public class PostCategory
    {
        public int PostId { get; set; }
        public Post Post { get; set; }

        public int CategoryId { get; set; }
        public Category Category { get; set; }


    }
}

PosrCreateVm

public class PostCreateVM
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public IEnumerable<Category> Categories { get; set; }
        public List<int> SelectedCategory { get; set; }
    }
}
namespace one.Controllers
{
    public class PostsController : Controller
    {
        private readonly AppDbContext dbContext;

        public PostsController(AppDbContext dbContext)
        {
            this.dbContext = dbContext;
        }
        public IActionResult Index()
        {
            var post = dbContext.Posts.OrderByDescending(s => s.PostId);
            return View(post);
        }

        [HttpGet]
        public IActionResult Details(int? id)
        {
            var post = dbContext.Posts.FirstOrDefault(m => m.PostId == id);

            return View(post);
        }

        [HttpGet]
        public IActionResult Create()
        {
            var post = new PostCreateVM();

            post.Categories = dbContext.Categories.ToList();
            ViewBag.Roles = new SelectList(dbContext.Categories, "CategoryId", "CategoryName");
            return View(post);

        }

        [HttpPost]

        public IActionResult Create([FromForm] PostCreateVM vm)
        {
            foreach (var item in vm.SelectedCategory)
            {
                PostCategory postCategory = new PostCategory()
                {
                    CategoryId = item
                };

            }

            Post post = new Post()
            {
                PostId = vm.PostId,
                Description = vm.Description,
                Title = vm.Title,
            };

            dbContext.Add(post);
            dbContext.SaveChanges();
            TempData["message"] = "Successfully Added";
            return RedirectToAction("Index");
        }

    }
}

0 个答案:

没有答案