سلام
من طبق آموزش جلو رفتم استاد بعد از خطا 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
سلام
اون مدلی که صفحه باهاش درست شده، اون مدلی نیستش که داری بهش پاس میدی.
مطمئن شو که هر دو مدل یکی هستن
سلام دوست عزیز:
این کدهای منه:
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 && 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));
}
اینها رو مد نظرم نبود،
موقعی که داری ویو درست میکنی، یه مدل انتخاب میکنی.
اون مدله با اون چیزی که قراره بهش پاس بدی باید یکی باشه
آقای اکبری والا متوجه نشدم میشه بیشتر توضیح بدی؟
کدوم ویو منظورته؟
زمانی که داری ویو اضافه میکنی به اکشن هر کنترلر، این پنجره رو میبینی.
تو بخش مدل کلاس باید ببینی که این مدل با اون مدلی که پاس میدی یکی باشه یا نه.
لطفا لطفا فیلم های استاد اگر لازمه، چندین بار نگاه کن تا به این مسائل به طور کامل مسلط بشی وگرنه کلاه ات پس معرکه است و لنگ کسی هستی که بیاد کمک ات کنه
برای من غیر فعاله
این برای همون ویو ایندکس در کنترلر کورس هست:
@using DrZahra.DataLayer.Entities.Course
@model Tuple<List<DrZahra.Core.DTOs.ShowCourseListItemViewModel>,int>
@{
ViewData["Title"] = "Index";
}
ویدیو استاد رو یه بار دیگه نگاه کنید تا متوجه بشید چرا خاموش هستش.
بخش کار با ویو ها و کنترلر