C# ASP.NET字典需要类型为NO SENSE的模型项

C# ASP.NET字典需要类型为NO SENSE的模型项,c#,asp.net,C#,Asp.net,我得到以下错误 传入字典的模型项的类型为“Vidly.ViewModels.CustomersViewModel”,但此字典需要“Vidly.ViewModels.RandomMovieViewModel”类型的模型项。 这毫无意义!与“Vidly.ViewModels.RandomMovieViewModel”无关,我只想在视图中传递“Vidly.ViewModels.CustomersViewModel”。 我的代码如下: 控制器 使用系统; 使用System.Collections.Gen

我得到以下错误

传入字典的模型项的类型为“Vidly.ViewModels.CustomersViewModel”,但此字典需要“Vidly.ViewModels.RandomMovieViewModel”类型的模型项。

这毫无意义!与“Vidly.ViewModels.RandomMovieViewModel”无关,我只想在视图中传递“Vidly.ViewModels.CustomersViewModel”。 我的代码如下:

控制器
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
使用Vidly.模型;
使用Vidly.ViewModels;
名称空间Vidly.Controllers
{
公共类另一个CustomerController:控制器
{
私有ApplicationDbContext_context=新ApplicationDbContext();
//获得:其他客户
[路线(“其他客户/索引”)]
公共行动结果索引()
{
var客户=新列表
{
新客户
{
Id=1,Name=“sdw”,
IsSubscribedToNewsLetter=false,
MemberShipTypeId=1,
MemberShipType=新的MemberShipType(),
MemberShipName=“待定”
},
新客户
{
Id=2,Name=“dws”,
IsSubscribedToNewsLetter=false,
MemberShipTypeId=2,
MemberShipType=新的MemberShipType(),
MemberShipName=“待定”
}
};
CustomerViewModel ViewModel=新CustomerViewModel
{
CustomerList=客户
};
返回视图(ViewModel);
}
}
}
CustomerViewModel
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用Vidly.模型;
命名空间Vidly.ViewModels
{
公共类CustomerViewModel
{
公共列表客户列表{get;set;}
}
}
看法
@model Vidly.ViewModels.CustomersViewModel
@{
ViewBag.Title=“Index”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
顾客
    @foreach(Model.CustomerList中的var c) {
  • @c、 名字
  • }
我很确定我分享的代码是正确的

我想通过路径“/AnotherCustomers/index”返回视图(在Views/AnotherCustomers文件夹下)

因此控制器必须是[AnotherCustomersController],操作必须是Index()

但是我不明白为什么视图总是需要“Vidly.ViewModels.RandomMovieViewModel”,它与其他CustomerController没有关系

以防万一,下面是与“Vidly.ViewModels.RandomMovieViewModel”相关的代码

电影导演
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
使用Vidly.模型;
使用Vidly.ViewModels;
名称空间Vidly.Controllers
{
公共类电影控制器:控制器
{
[Route(“movies/released/{year:Regex(\\d{4}):range(1991、2018)}/{month:Regex(\\d{2}):range(1、12)}”)]
公共行动结果ByReleasedDate(整数年,整数月)
{
退货内容(年+“/”+月);
}
公共行动(电影)
{
var movie=new movie(){Name=“WTF”};
var客户=新列表
{
新客户{Name=“Stoneway”},
新客户{Name=“YourDaddy”}
};
var viewModel=新的随机电影视图模型
{
电影,
顾客=顾客
};
返回视图(viewModel);
}
}
}
随机电影视图模型
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用Vidly.模型;
命名空间Vidly.ViewModels
{
公共类随机电影视图模型
{
公共电影{get;set;}
公共列表客户{get;set;}
}
}
随机电影视图
@model Vidly.ViewModels.RandomMovieViewModel
@{
ViewBag.Title=“随机电影”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
@{ 
var className=Model.Customers.Count>5?“流行”:空;
}
@Model.Movie.Name
@如果(Model.Customers.Count==0)
{
以前没有人租过这部电影

} 其他的 {
    @foreach(Model.Customers中的var c) {
  • @c、 名字
  • }
}
我仍然不明白为什么它需要另一个ViewModel

但是,我通过另一种不受欢迎的方式解决了这个问题

我在Controller中使用了ViewData字典,而不是将ViewModel传递给View

控制器(ViewDataDictionary) 视图(ViewDataDictionary) 视图(视图包)
@使用Vidly.ViewModels
@模型Vidly.ViewModels.CustomersViewModel
@{
ViewBag.Title=“Index”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
客户
    @foreach(变量c在((CustomerViewModel)ViewBag.AnotherCustomers.CustomerList中) {
  • @c、 名字
  • }
问题已解决

这以前从未发生过,在决定删除这个该死的ViewModel之前,我永远不知道如何修复它

我不知道何时以及如何添加模型引用

@model Vidly.ViewModels.RandomMovieViewModel

这就是为什么它一直要求我通过这个模型的原因


删除无用的引用后,一切恢复正常。

视图代码如何?作为
@Model
,它有什么功能?您的视图存储在哪里?您可以在执行返回视图时尝试指定位置(…是否确实加载了正确的视图?视图放置在正确的文件夹中?该视图位于哪个目录中?共享其他控制器操作和视图。我不认为您在此处使用了正确的操作/视图或共享的正确代码,因此,您没有解决问题,yo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Vidly.Models;
namespace Vidly.ViewModels
{
    public class CustomersViewModel
    {
        public List<Customer> CustomerList { get; set; }
    }
}
@model Vidly.ViewModels.CustomersViewModel
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Customer</h2>
<ul>
    @foreach (var c in Model.CustomerList)
    {
        <li>@c.Name</li>
    }
</ul>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Vidly.Models;
using Vidly.ViewModels;

namespace Vidly.Controllers
{
    public class MoviesController : Controller
    {
        [Route("movies/released/{year:Regex(\\d{4}):range(1991, 2018)}/{month:Regex(\\d{2}):range(1,12)}")]
        public ActionResult ByReleasedDate(int year, int month)
        {
            return Content(year + "/" + month);
        }

        public ActionResult RandomMovie()
        {
            var movie = new Movie() { Name = "WTF" };
            var customers = new List<Customer>
            {
                new Customer { Name = "Stoneway"},
                new Customer { Name = "YourDaddy"}
            };

            var viewModel = new RandomMovieViewModel
            {
                Movie = movie,
                Customers = customers
            };
            return View(viewModel);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Vidly.Models;

namespace Vidly.ViewModels
{
    public class RandomMovieViewModel
    {
        public Movie Movie { get; set; }
        public List<Customer> Customers { get; set; }
    }
}
@model Vidly.ViewModels.RandomMovieViewModel
@{
    ViewBag.Title = "RandomMovie";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@{ 
    var className = Model.Customers.Count > 5 ? "Popular" : null;
}

<h2 class="@className">@Model.Movie.Name</h2>
@if (Model.Customers.Count == 0)
{
    <p>No One has rent this movie before.</p>
}
else
{
    <ul>
        @foreach (var c in Model.Customers)
        {
            <li>@c.Name</li>
        }
    </ul>
}
    CustomersViewModel ViewModel = new CustomersViewModel
    {
        CustomerList = customers
    };

    ViewData["AnotherCustomers"] = ViewModel;

    return View();
@using Vidly.ViewModels
@model Vidly.ViewModels.CustomersViewModel
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Customers</h2>
<ul>
@foreach (var c in ((CustomersViewModel)ViewData["AnotherCustomers"]).CustomerList)
{
    <li>@c.Name</li>
}
</ul>
    CustomersViewModel ViewModel = new CustomersViewModel
    {
        CustomerList = customers
    };

    ViewBag.AnotherCustomers = ViewModel;

    return View();
@using Vidly.ViewModels
@model Vidly.ViewModels.CustomersViewModel
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Customers</h2>
<ul>
@foreach (var c in ((CustomersViewModel)ViewBag.AnotherCustomers).CustomerList)
{
    <li>@c.Name</li>
}
</ul>
@model Vidly.ViewModels.RandomMovieViewModel
_Layout.cshtml