【问题标题】:ASP.NET dictionary requires a model item of type NO SENSEASP.NET 字典需要 NO SENSE 类型的模型项
【发布时间】:2018-03-13 07:47:38
【问题描述】:

我收到以下错误。

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

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

控制器

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 AnotherCustomersController : Controller
    {
        private ApplicationDbContext _context = new ApplicationDbContext();

        // GET: AnotherCustomers
        [Route("anothercustomers/index")]
        public ActionResult Index()
        {
            var customers = new List<Customer>
            {
                new Customer
                {
                    Id = 1, Name = "sdw",
                    IsSubscribedToNewsLetter = false,
                    MemberShipTypeId = 1,
                    MemberShipType = new MembershipType(),
                    MemberShipName = "TBD"
                },

                new Customer
                {
                    Id = 2, Name = "dws",
                    IsSubscribedToNewsLetter = false,
                    MemberShipTypeId = 2,
                    MemberShipType = new MembershipType(),
                    MemberShipName = "TBD"
                }
            };
            CustomersViewModel ViewModel = new CustomersViewModel
            {
                CustomerList = customers
            };
            return View(ViewModel);
        }
    }
}

客户视图模型

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>

我很确定我分享的代码是正确的。

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

所以Controller必须是[AnotherCustomersController],action必须是Index()。

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

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

电影控制器

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);
        }
    }
}

RandomMovieViewModel

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; }
    }
}

RandomMovieView

@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>
}

【问题讨论】:

  • 查看代码呢? @Model 是什么?
  • 您的视图存储在哪里?您可以尝试在执行 return View(... 时指定位置
  • 您确定您正在加载正确的视图吗?视图放置在正确的文件夹中?
  • 该视图驻留在哪个目录中?
  • 分享其他控制器动作和视图。我不认为你在这里使用正确的操作/视图或共享正确的代码

标签: c# asp.net


【解决方案1】:

问题解决了。

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

我不知道何时以及如何添加模型参考

@model Vidly.ViewModels.RandomMovieViewModel

_Layout.cshtml 

这就是为什么它一直要求我在视图中传递该模型。

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

【讨论】:

  • 我也在遵循相同的教程,我也犯了同样的错误。感谢您提供信息。
【解决方案2】:

我仍然很困惑为什么它需要另一个 ViewModel。

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

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

控制器(查看数据字典)

    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>

完美解决。但是有点丑。或者使用 ViewBag:

控制器(ViewBag)

    CustomersViewModel ViewModel = new CustomersViewModel
    {
        CustomerList = customers
    };

    ViewBag.AnotherCustomers = ViewModel;

    return View();

视图(ViewBag)

@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>

【讨论】:

  • 所以你没有解决问题,只是耸耸肩,做了一些不可取的事情,而且肯定会在未来引起维护问题?干得好。
  • 首先感谢您的帮助。我不想和你争论,但有时人们需要一个可选的解决方案来推动一个对时间敏感的项目。给出一两个选项并不意味着我放弃了这个问题。欢迎提供更多解决方案,谢谢。
猜你喜欢
  • 1970-01-01
  • 2017-11-26
  • 2020-09-08
  • 1970-01-01
  • 2022-01-16
  • 2013-10-18
  • 2015-09-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多