Edycja poprzez Checkbox lub MultiSelectList MVC4

0

Witam,

Posiadam dwie tabele z relacją wiele do wielu:

user image

Obie tabele mają swój model wygenerowany poprzez EF DBGenerate Context.

ANIME:

 
    using System;
    using System.Linq;
    using System.Collections.Generic;
    
    public partial class ANIME
    {
        public ANIME()
        {
            this.GENRES = new HashSet<GENRES>();
        }
    
        public int ID_AN { get; set; }
        public string SMALL_PIC { get; set; }
        public string TITLE_OR { get; set; }
        public string TITLE_EN { get; set; }
        public string TYPE { get; set; }
        public Nullable<int> EPISODES { get; set; }
        public Nullable<System.DateTime> AIRED_START { get; set; }
        public Nullable<System.DateTime> AIRED_END { get; set; }
        public string SYNOPSIS { get; set; }
        public Nullable<int> RATING { get; set; }
        public string OP_THEME { get; set; }
        public string ED_THEME { get; set; }
    
        public virtual ICollection<GENRES> GENRES { get; set; }
    }

GENRES:

 
    using System;
    using System.Collections.Generic;
    
    public partial class GENRES
    {
        public GENRES()
        {
            this.ANIME = new HashSet<ANIME>();
            var AllGenres = new List<GENRES>();
        }
    
        public int ID_GE { get; set; }
        public string GENRE { get; set; }
        public virtual ICollection<ANIME> ANIME { get; set; }
    }

W kontrolerze dodałem linijki odpowiedzialne za edycje:

public ActionResult Edit(int id)
        {
            using (var db = new MainDatabaseEntities())
            {
                return View(db.ANIME.Include("GENRES").FirstOrDefault(a => a.ID_AN == id));
            }
        }
        [HttpPost]
        public ActionResult Edit(int id, ANIME anime)
        {
            try
            {
                using (var db = new MainDatabaseEntities())
                {
                    db.Entry(anime).State = System.Data.EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            catch
            {
                return View();
            }
        }
 

Do tego stworzyłem też view. Niestety tworząc w ten sposób wyświetlane są dane które już są przypisane do danego anime. Chciałbym jednak by mi wyświetlało wszystkie GENRE tak bym mógł za pomocą checkboxów lub listy wielokrotnego wyboru je dowolnie zmieniać. Wie ktoś jak tego dokonać i mógłby się tą wiedzą podzielić?

0

Utwórz sobie klasę (viewmodel), która będzie zawierała obiekt Anime i kolekcję wszystkich obiektów Genres, i niech ta klasa będzie modelem dla Twojego widoku. W kontrolerze wczytaj odpowiednie dane i utwórz obiekt viewmodelu.

P.S. Oczy bolą od tego capslocka.

1 użytkowników online, w tym zalogowanych: 0, gości: 1