传递给ViewDataDictionary的模型项的类型为,但是此ViewDataDictionary实例需要类型为的模型项

时间:2019-07-24 05:21:37

标签: asp.net

传递给ViewDataDictionary的模型项的类型为'DrinkAndGo.ViewModels.HomeVM',但是此ViewDataDictionary实例需要一个类型为'DrinkAndGo.ViewModels.DrinkListVM'的模型项。

我读了这篇文章:The model item passed into the dictionary is of type .. but this dictionary requires a model item of type,但我认为这与我的问题不同。

在_ViewImport.cshtml文件中

@using DrinkAndGo.Models
@using DrinkAndGo

@model DrinkAndGo.ViewModels.DrinkListVM
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

主视图模型

public class HomeVM
    {
        public IEnumerable<Drink> PreferredDrinks { get; set; }
    }

饮料视图模型

public class DrinkListVM
    {
        public IEnumerable<Drink> Drinks { get; set; }
        public string CurrentCategory { get; set; }
    }

首页/索引

@model DrinkAndGo.ViewModels.HomeVM

@await Html.PartialAsync("Carousel")

<h2>Prefered drinks of the week </h2>
<h4>Clients preferences - just for you!</h4>

<div class="row">
    @foreach (var drink in Model.PreferredDrinks)
    {
        @Html.Partial("DrinksSummary", drink)
    }
</div>

DrinksSummary.cshtml

@model DrinkAndGo.Models.Drink

<div class="col-sm-4 col-lg-4 col-md-4">
    <div class="thumbnail">
        <img src="@Model.ImageThumbnailUrl" alt="" />
        <div class="caption">
            <h3 class="pull-right">@Model.Price.ToString("c")</h3>
            <h3>
                <a asp-controller="Drink" asp-action="Details" asp-route-drinkId="@Model.DrinkId">@Model.Name</a>
            </h3>
            <p>@Model.ShortDescription</p>
        </div>
        <div class="addToCart text-right">
            <p class="button ">
                <a class="btn btn-success" id="cartButton" asp-controller="ShoppingCart" asp-action="AddToShoppingCart" asp-route-drinkId="@Model.DrinkId">
                    Add to cart
                </a>
            </p>
        </div>
    </div>
</div>

Carousel.cshtml

<div class="row carousel-holder marginTop1">
    <div class="col-md-12">
        <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
            <ol class="carousel-indicators">
                <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
                <li data-target="#carousel-example-generic" data-slide-to="1"></li>
                <li data-target="#carousel-example-generic" data-slide-to="2"></li>
            </ol>
            <div class="carousel-inner">
                <div class="item active">
                    <img class="slide-image" src="https://raw.githubusercontent.com/etrupja/DrinkAndGo/master/src/DrinkAndGo/wwwroot/Images/carousel1.jpg" alt="">
                </div>
                <div class="item">
                    <img class="slide-image" src="https://raw.githubusercontent.com/etrupja/DrinkAndGo/master/src/DrinkAndGo/wwwroot/Images/carousel2.jpg" alt="">
                </div>
                <div class="item">
                    <img class="slide-image" src="https://raw.githubusercontent.com/etrupja/DrinkAndGo/master/src/DrinkAndGo/wwwroot/Images/carousel3.jpg" alt="">
                </div>
            </div>
            <a class="left carousel-control" href="#carousel-example-generic" data-slide="prev">
                <span class="glyphicon glyphicon-chevron-left"></span>
            </a>
            <a class="right carousel-control" href="#carousel-example-generic" data-slide="next">
                <span class="glyphicon glyphicon-chevron-right"></span>
            </a>
        </div>
    </div>
</div>

HomeController

public class HomeController : Controller
    {
        private readonly IDrinkRepository _drinkRepository;
        public HomeController(IDrinkRepository drinkRepository)
        {
            _drinkRepository = drinkRepository;
        }

        public ViewResult Index()
        {
            var homeVM = new HomeVM
            {
                PreferredDrinks = _drinkRepository.PreferredDrinks
            };

            return View(homeVM);
        }
    }

0 个答案:

没有答案