Asp.Net Mvc4 Add multiple saved entities to an entity on a create view with multi select RRS feed

  • Frage

  • Hello,

    i have the following entities: Artikel and Photo

    A article entity which can has multiple photo entities. The photo entities can be added and saved in the database on an extra view.

    That means i can have had multiple photos in the database but no article yet.

    So if i want to create an article i would be able to choose multiple photos from a list box. How can i do that?

    i have the following models:

        public class Artikel
            public long ArtikelID { get; set; }
            public string Beschreibung { get; set; }
            public double Preis { get; set; }
            public int Stueck { get; set; }
            public string Artikeltyp { get; set; }
            public bool Verfuegbar { get; set; }
            public virtual Ersatzteile Ersatzteil { get; set; }
            public virtual Felgen Felgen { get; set; }
            public virtual Fahrzeug Fahrzeug { get; set; }
            public virtual Sonstiges Sonstiges { get; set; }
            public virtual Reifen Reifen { get; set; }
            public virtual ICollection<Foto> Fotos { get; set; }

        public class Foto
            public long FotoID { get; set; }
            public string Fototyp { get; set; }
            public DateTime Uploaddatum { get; set; }
            public double SizeKB { get; set; }
            public string Pfad800X600 { get; set; }
            public string PfadThumbnail { get; set; }
            public string PfadMedium { get; set; }
            public string Name { get; set; }
            public string Beschreibung { get; set; }
            public string ImageUrl { get; set; }
            [Display(Name="Als Artikelfoto markieren?", Description="Nur Artikelfotos können beim hinzufügen eines neuen Artikels ausgewählt werden.")]
            public bool IstArtikelFoto { get; set; }
            public virtual Artikel Artikel { get; set; }
            public virtual News News { get; set; }
            public virtual Mitarbeiter Mitarbeiter { get; set; }
            //[FileExtensions(Extensions=(".jpg",".jpeg",".png"),ErrorMessage="Datei hat ein falsches Format. (Erforderlich: jpg, jpeg, png")]
            public HttpPostedFileBase FotoContent { get; set; }
            public bool Delete { get; set; }

    and for the context:

            protected override void OnModelCreating(DbModelBuilder modelBuilder)
                    modelBuilder.Entity<Fahrzeug>().Property(f => f.Baujahr).HasColumnType("datetime2").HasPrecision(0);
                    modelBuilder.Entity<News>().Property(f => f.Erstelldatum).HasColumnType("datetime2").HasPrecision(0);
                    modelBuilder.Entity<Foto>().Property(f => f.Uploaddatum).HasColumnType("datetime2").HasPrecision(0);
                    modelBuilder.Entity<Marke>().HasMany(j => j.Modelle).WithRequired(m => m.Marke).WillCascadeOnDelete(true);
                    modelBuilder.Entity<Modell>().HasMany(j => j.Artikeln).WithRequired(m => m.Modell).WillCascadeOnDelete(true);
                    modelBuilder.Entity<Artikel>().HasMany(j => j.Fotos).WithOptional(m => m.Artikel).WillCascadeOnDelete(true);
                    modelBuilder.Entity<Ersatzteile>().HasRequired(j => j.Artikel).WithOptional(m => m.Ersatzteil).WillCascadeOnDelete(true);
                    modelBuilder.Entity<Fahrzeug>().HasRequired(j => j.Artikel).WithOptional(m => m.Fahrzeug).WillCascadeOnDelete(true);
                    modelBuilder.Entity<Felgen>().HasRequired(j => j.Artikel).WithOptional(m => m.Felgen).WillCascadeOnDelete(true);
                    modelBuilder.Entity<Reifen>().HasRequired(j => j.Artikel).WithOptional(m => m.Reifen).WillCascadeOnDelete(true);
                    modelBuilder.Entity<Sonstiges>().HasRequired(j => j.Artikel).WithOptional(m => m.Sonstiges).WillCascadeOnDelete(true);
                catch (Exception ex)
                    throw ex;

    i have a strongly typed view (Artikel) for creating a new article.

    There i tried to pass a List with photos:

                var fotos = db.Fotos.Where(m => m.IstArtikelFoto).Select(m => m).ToList();
                ViewData["Fotos"] = new SelectList(fotos, "FotoID", "Name");

    and in the view i tried to display it as listbox:

    @Html.ListBoxFor( m => m.Fotos, new MultiSelectList(ViewData["Fotos"] as SelectList, "FotoID", "Name"))

    when i submit the form i get 0 photo items.

    I hope i could explain my problem understandable.

    Any ideas how i can solve this problem?



    Liebe Gr+ße Michael Web Entwickler

    Dienstag, 29. Januar 2013 13:04

Alle Antworten

  • Doppelter Post:


    Donnerstag, 13. Juni 2013 07:25