• 1400/01/28

گرفتن خطا از لیست tuple :

سلام 

من طبق آموزش جلو رفتم  استاد بعد از خطا Ienumrable  رو تبدیل به لیست کرد و مشکل حل شد منم همین کارو میکنم ولی متاسفانه بازم خطا میده خیلیم سرچ کردم حقیقت چیزی دستگیرم نشد اگر کسی این مشکل ور حل کرده لطفا راهنمایی کنه 

InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Tuple`2[System.Collections.Generic.List`1[Core.DTOs.Course.ShowCourseListItemViewModel],System.Int32]', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.IEnumerable`1[Core.DTOs.Course.ShowCourseListItemViewModel
  • 1400/01/29
  • ساعت 10:42

سلام
اون مدلی که صفحه باهاش درست شده، اون مدلی نیستش که داری بهش پاس میدی.

مطمئن شو که هر دو مدل یکی هستن


  • 1400/01/29
  • ساعت 12:00

سلام دوست عزیز:

این کدهای منه:

 

 

  public class ShowCourseListItemViewModel
    {
        public int CourseId { get; set; }
        public string CourseTitle { get; set; }
        public string ImageName { get; set; }
        public string EducatorName { get; set; }
        public TimeSpan CourseTime { get; set; }
        public int Price { get; set; }
        }

 

CourseService:
public Tuple<List<ShowCourseListItemViewModel>, int> GetCourse(int pageId = 1, string filter = "", string getType = "All", string orderByType = "Date",
            List<int> selectedGroup = null, int take = 0)
        {
            if (take == 0)
                take = 8;
            IQueryable<Course> result = _context.Courses;
            if (!string.IsNullOrEmpty(filter))
            {
                result = result.Where(c => c.CourseTitle.Contains(filter));
            }

            switch (getType)
            {
                case "All":
                    break;
                case "price":
                    {
                        result = result.Where(c => c.CoursePrice != 0);
                        break;
                    }
                case "free":
                    {
                        result = result.Where(c => c.CoursePrice == 0);
                        break;
                    }

            }

            switch (orderByType)
            {
                case "All":
                    break;
                case "Data":
                    {
                        result = result.OrderByDescending(c => c.CreateDate);
                        break;
                    }
                case "UpData":
                    {
                        result = result.OrderByDescending(c => c.UpdateDate);
                        break;
                    }
                case "Free":
                    {
                        result = result.Where(c => c.CoursePrice == 0);
                        break;
                    }

                case "Buy":
                    {
                        result = result.Where(c => c.CoursePrice != 0);
                        break;
                    }

            }




            if (selectedGroup != null &amp;&amp; selectedGroup.Any())
            {
                result = selectedGroup.Aggregate(result, (current, groupId) => current.Where(c => c.GroupId == groupId || c.SubGroupId == groupId || c.SecondSubGroupId == groupId));
            }

            var skip = (pageId - 1) * take;
            var pageCount = result.Include(e => e.CourseEpisodes).Include(u => u.User).Select(c =>
                new ShowCourseListItemViewModel()
                {
                    CourseId = c.CourseId,
                    ImageName = c.CourseImageName,
                    Price = c.CoursePrice,
                    CourseTitle = c.CourseTitle,
                    EducatorName = c.User.UserName,
                    //  CourseTime = new TimeSpan(c.CourseEpisodes.Sum(e => e.EpisodeTime.Ticks))
                }).Count() / take;
            var query = result.Include(e => e.CourseEpisodes).Include(u => u.User).Select(c =>
                new ShowCourseListItemViewModel()
                {

                    CourseId = c.CourseId,
                    ImageName = c.CourseImageName,
                    Price = c.CoursePrice,
                    CourseTitle = c.CourseTitle,
                    EducatorName = c.User.UserName,
                    //  CourseTime = new TimeSpan(c.CourseEpisodes.Sum(e => e.EpisodeTime.Ticks))

                }).Skip(skip).Take(take).ToList();
            return Tuple.Create(query, pageCount);

 

 

IcourseService:

     Tuple<List<ShowCourseListItemViewModel>, int> GetCourse(int pageId = 1, string filter = "",
            string getType = "All", string orderByType = "Date",
            List<int> selectedGroup = null, int take = 0);

index کنترلر:

@model Tuple<List<DrZahra.Core.DTOs.ShowCourseListItemViewModel>, int>

متد کنترلر:

public IActionResult Index(int pageId = 1, string filter = "", string getType = "All", string orderByType = "Date",List<int> selectedGroup = null)
        {
            ViewBag.selectedgroups = selectedGroup;
            ViewBag.groups = _courseService.GetAllGroups();
            ViewBag.getType = getType;
            ViewBag.pageId = pageId;
            
            return View(_courseService.GetCourse(pageId, filter, getType, orderByType, selectedGroup,9));
        }

  • 1400/01/29
  • ساعت 12:03

اینها رو مد نظرم نبود،

موقعی که داری ویو درست میکنی، یه مدل انتخاب میکنی. 

اون مدله با اون چیزی که قراره بهش پاس بدی باید یکی باشه


  • 1400/01/29
  • ساعت 12:15

آقای اکبری والا متوجه نشدم میشه بیشتر توضیح بدی؟

کدوم ویو منظورته؟


  • 1400/01/29
  • ساعت 12:20
VS2017 ASP .NET Core Error when adding a Razor View - Stack Overflow

زمانی که داری ویو اضافه میکنی به اکشن هر کنترلر، این پنجره رو میبینی.
تو بخش مدل  کلاس باید ببینی که این مدل با اون مدلی که پاس میدی یکی باشه یا نه.

لطفا لطفا فیلم های استاد اگر لازمه، چندین بار نگاه کن تا به این مسائل به طور کامل مسلط بشی وگرنه کلاه ات پس معرکه است و لنگ کسی هستی که بیاد کمک ات کنه


  • 1400/01/29
  • ساعت 12:23

برای من غیر فعاله


  • 1400/01/29
  • ساعت 12:28

این برای همون ویو ایندکس در کنترلر کورس هست:

@using DrZahra.DataLayer.Entities.Course
@model Tuple<List<DrZahra.Core.DTOs.ShowCourseListItemViewModel>,int>
@{
    ViewData["Title"] = "Index";
}

  • 1400/01/29
  • ساعت 12:29

ویدیو استاد رو یه بار دیگه نگاه کنید تا متوجه بشید چرا خاموش هستش.
بخش کار با ویو ها و کنترلر


logo-samandehi