ASP/C# | .Include()

0

Witam,
staram się zrobić prostą aplikacje i mam problem z jedną rzeczą.

        public ActionResult KindsList(string kindname)
        {
            var kind = db.Kinds.Include("Movie").Where(k => k.KindName.ToUpper() == kindname.ToUpper()).Single();
            var movies = kind.Movies.ToList();
            return View(movies);
        }

Gdy mam .Include("Movies") to widok nie działa gdyż mam błąd:

Additional information: A specified Include path is not valid. The EntityType 'Isf.DataAccess.Kind' does not declare a navigation property with the name 'Movie'.

i wartość kindname jest pusta.
Natomiast gdy usunę .Include("Movies") i dodam w url ?kindname=horror

        public ActionResult KindsList(string kindname)
        {
            var kind = db.Kinds.Where(k => k.KindName.ToUpper() == kindname.ToUpper()).Single();
            var movies = kind.Movies.ToList();
            return View(movies);
        }

aplikacja zwraca mi taki błąd:

Additional information: Wartość nie może być zerowa.

w tym wypadku kindname ma wartość "komedia" ale juz movies = null. Rozumiem, że "nie widzi" Movies gdyż pobiera Kinds bez kolekcji Movies... Tylko jak temu zaradzić ?

Klasa Kind:

    public class Kind
    {
        public string KindName { get; set; }
        public int KindId { get; set; }
        public ICollection<Movie> Movies { get; set; }
    }

Klasa Movie:

    public class Movie
    {
        public string MovieName { get; set; }
        public string MovieText { get; set; }
        public string MovieDirector { get; set; }
        public decimal MoviePrice { get; set; }
        public int MovieId { get; set; }
        public int KindId { get; set; }
        public string Cover { get; set; }
        public bool IsOrNot { get; set; }
        public bool Recommended { get; set; }
        public DateTime Added { get; set; }
        public virtual Kind Kind { get; set; }
    }

DbSety:

        public DbSet<Movie> Movies { get; set; }
        public DbSet<Kind> Kinds { get; set; }

Pozdrawiam

1

Ja tam widze w kodzie u Ciebie "Movie" zamiast "Movies"
Do tego możesz to napisać używając lambd, tak:

...
db.Kinds.Include(kind => kind.Movies) 
...
0

A można poprosić o jakąś podpowiedź ? bo w wielu miejscach próbowałem to zmienić jak i próbowałem napisać tak jak sugerujesz i niestety dalej to samo...
Nie proszę o gotową odpowiedź ale walczę z tym już parę dni...

0

Witam,
zmieniłem "Movie" na "Movies" i teraz mam taki błąd po przekazaniu parametru kindname:

Błąd serwera w aplikacji '/'.
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Isf.Models.Movie]', but this dictionary requires a model item of type 'Isf.ViewModels.HomeViewModel'.

Robiłem juz kilka aplikacji w ten sam sposób i nigdy nie prosiło mnie o ViewModel...

0

A co masz w widoku?

0

To:


@{
    ViewBag.Title = "KindsList";
    Layout = "~/Views/Shared/_StoreLayout.cshtml";
}

<div class="container">
    <div class="row">
        <div class="col-xs-12 col-md-2" id="right-list">
             @Html.Action("KindsMenu")
        </div>
        <div class="col-xs-12 col-md-10">
            @Html.Partial("_MovieList") **<--- tu wywala błąd**
        </div>
    </div>
</div>

a jeżeli usunę wywołanie Html.Partial to wywala w tym miejscu:

        public ActionResult KindsList(string kindname)
        {
            var kinds = db.Kinds.Include("Movies").Where(k => k.KindName.ToUpper() == kindname.ToUpper()).Single(); **<--- tutaj**
            var movies = kinds.Movies.ToList(); 
            return View(movies);
        }

a w przyglądarce zwraca mi ten błąd:

An exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not handled in user code

Additional information: The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Isf.Models.Movie]', but this dictionary requires a model item of type 'Isf.Models.Movie'.

W kursie uczyłem się, że gdy dodam przez html.action widok cząstkowy to nie musze mieć view modelu... i w kursie to działa...

0

Ehh - to znaczy że do któregoś widoku lub partiala, przekazujesz obiekt, a w zasadzie kolekcję nie tego typu którego się spodziewa. To czego się spodziewa dany widok deklarujesz zwykle w 1 linijce na górze jako model:

 @model Movie

albo @model Isf.Models.MovieMovie

w zależności czy zna namespace czy nie.

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