传递到字典中的模型项的类型为'System.Collections.Generic.List`1

时间:2016-02-18 15:52:04

标签: asp.net-mvc asp.net-mvc-5

传递到字典中的模型项的类型为System.Collections.Generic.List1[Web.ViewModel.HomePageView],但此字典需要类型为Web.ViewModel.HomePageView的模型项。

主页

public class HomePageView
{
    public List<Category> Category { get; set; }
    public List<Slider> Slider { get; set; }
    public List<SliderImage> SliderImage { get; set; }
    public List<Contact> Contact { get; set; }
    public List<Setting> Setting { get; set; }

}

的HomeController

namespace Web.Controllers
{
    public class HomeController : Controller
    {

        private readonly ISliderRepository _sliderRepository;
        private readonly ISliderImageRepository _sliderImageRepository;
        private readonly ICategoryRepository _categoryRepository;
        private readonly IContactRepository _contactRepository;
        private readonly ISettingRepository _settingRepository;

        public HomeController(ISliderRepository sliderRepository, ISliderImageRepository sliderImageRepository, ICategoryRepository categoryRepository, IContactRepository contactRepository, ISettingRepository settingRepository)
        {
            _sliderRepository = sliderRepository;
            _sliderImageRepository = sliderImageRepository;
            _categoryRepository = categoryRepository;
            _contactRepository = contactRepository;
            _settingRepository = settingRepository;
        }

        public ActionResult Index()
        {
            var slider = _sliderRepository.GetAll().ToList();
            var sliderImg = _sliderImageRepository.GetAll().ToList();
            var category = _categoryRepository.GetAll().ToList();
            var contact = _contactRepository.GetAll().ToList();
            var setting = _settingRepository.GetAll().ToList();

            var Model = new HomePageView
            {
                Slider = slider,
                SliderImage = sliderImg,
                Category = category,
                Contact = contact,
                Setting = setting
            };

            List<HomePageView> newList = new List<HomePageView>();
            newList.Add(Model);

            return View(newList);
        }
    }
}

Index.cshtml

@model Web.ViewModel.HomePageView
@{
    ViewBag.Title = "başlık ";
}
@Html.Partial("Slider", Model.Slider)

Slider.cshtml

@using Web.Helpers
@model System.Collections.Generic.IEnumerable<Web.ViewModel.HomePageView>

@foreach (var db in Model.SelectMany(x => x.Slider))
{
    if (Model.SelectMany(x => x.Slider).Any())
    {
        <div class="carousel-inner">

            <div class="carousel-item active">

                @{
                    var sliderFirstImage = db.SliderImages.FirstOrDefault();
                    if (sliderFirstImage != null)
                    {
                        <img src="@Html.Base64SliderImage(sliderFirstImage)" srcset="@Html.Base64SliderImage(sliderFirstImage) 1920w, @Html.Base64SliderImage(sliderFirstImage) 425w" sizes="100vw" alt="">
                    }
                }

                <!--<img src="images/slider2.jpg" srcset="~/Content/images/slider2.jpg 1920w, ~/Content/images/slider2-425x146.jpg 425w" sizes="100vw" alt="The Best Construction WordPress Theme">-->
                <div class="container">
                    <div class="jumbotron-content">
                        <h1 class="jumbotron-content__title">@db.SliderTitle</h1>
                        <div class="jumbotron-content__description">
                            <p>
                                @db.SliderSubTitle
                            </p>
                            <p>
                                <a class="btn btn-primary" href="@db.Link1" target="_self">DAHA FAZLA</a>
                                <a class="btn btn-tertiary" href="@db.Link2" target="_self">İLETİŞİME GEÇ</a>
                            </p>
                        </div><!-- /.jumbotron-content__description -->
                    </div><!-- /.jumbotron-content -->
                </div><!-- /.container -->
            </div><!-- /.carousel-item -->

        </div><!-- /.carousel-inner -->
    }

}

1 个答案:

答案 0 :(得分:0)

我认为有两件事是错的:

首先,您将List<HomePageView>传递给Index.cshtml,后者需要一个HomePageView个实例。

您还将List<Slider>传递给部分途径@Html.Partial("Slider", Model.Slider),但它需要System.Collections.Generic.IEnumerable<Web.ViewModel.HomePageView>(正如您在@model定义的文件顶部定义的那样)。

一旦您将要发送到视图的内容与他们期望的内容同步,您就应该减少错误。将模型传递给索引,然后传递滑块集合可能更好,如果你想要做我认为你做的事情。尝试在控制器和视图之间保持干净的模型,当你开始处理帖子时,它会让生活更轻松。