【问题标题】:Why I obtain this "InvalidOperationException" when I try to render this .NET view?为什么我在尝试呈现此 .NET 视图时会获得此“InvalidOperationException”?
【发布时间】:2018-10-20 15:32:02
【问题描述】:

我是 .NETC#(我来自 Java 和 Spring 框架)的新手,在学习教程时遇到了一些问题。

我有一个简单的 controller 类:

namespace Vidly.Controllers
{
    public class CustomersController : Controller
    {
        public ViewResult Index()
        {
            var customers = GetCustomers();

            return View(customers);
        }

        public ActionResult Details(int id)
        {
            System.Diagnostics.Debug.WriteLine("Into Details()");
            var customer = GetCustomers().SingleOrDefault(c => c.Id == id);
            System.Diagnostics.Debug.WriteLine("customer: " + customer.Id + " " + customer.Name);

            if (customer == null)
                return HttpNotFound();

            return View(customer);
        }

        private IEnumerable<Customer> GetCustomers()
        {
            return new List<Customer>
            {
                new Customer { Id = 1, Name = "John Smith" },
                new Customer { Id = 2, Name = "Mary Williams" }
            };
        }
    }
}

你可以看到这个类包含这个 Details(int id) 方法:

public ActionResult Details(int id)
{
    System.Diagnostics.Debug.WriteLine("Into Details()");
    var customer = GetCustomers().SingleOrDefault(c => c.Id == id);
    System.Diagnostics.Debug.WriteLine("customer: " + customer.Id + " " + customer.Name);

    if (customer == null)
        return HttpNotFound();

    return View(customer);
}

因此,此方法处理 HTTP 类型的 GET 对 URL 的请求,例如:

localhost:62144/Customers/Details/1

它似乎可以工作,因为在输出控制台中我获得了 Into Details() 日志。另一个日志还解释了 customer 模型对象已正确初始化,事实上我得到了这个控制台输出:

customer: 1 John Smith

然后控制器返回一个 ViewResult 对象(调用 View 方法)包含之前的模型对象。

我认为 .NET 会自动尝试将此 ViewResult 对象(包含模型)发送到与处理此请求的控制器方法同名的视图。所以我有这个 Details.cshtml 视图:

@model Vidly.Models.Customer

@{
    ViewBag.Title = Model.Name;
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>@Model.Name</h2>

理论上应该接收这个 ViewResult 对象,从这里提取模型对象(具有 Vidly.Models.Customer 作为类型),它应该打印此模型对象的名称属性。

问题是我得到的是这个异常,而不是带有预期数据的预期页面:

[InvalidOperationException: The model item passed into the dictionary is of type 'Vidly.Models.Customer', but this dictionary requires a model item of type 'Vidly.ViewModels.RandomMovieViewModel'.]

为什么?什么意思?

Vidly.ViewModels.RandomMovieViewModel 是另一个模型对象,用于另一个控制器和另一个视图。

有什么问题?我错过了什么?我该如何解决这个问题?

【问题讨论】:

  • 因为你传递的参数是错误的(派生的)类型。通常您会尝试选择 Function 参数,以便只能给出正确的类型。这样,编译器甚至可以在您点击“编译”之前为您进行检查。但这并不总是可能的。如果这是不可能的,你必须对你得到的东西做一些is 检查。如果这不符合您的喜好,则必须抛出异常。
  • @Christopher mmm 我怀疑我没有明白你的意思......
  • @AndreaNobili 您的Details.cshtml 文件是否位于Views/Customers 目录中?看起来 MVC 引擎选择了错误的视图文件。
  • @AndreaNobili 您的 _Layout.cshtml 是否呈现需要 RandomMovieViewModel 的电影视图?或者,如果您的详细信息视图正在部分呈现该电影视图?
  • @AndreaNobili 这可能就是原因。您是否需要 _Layout.cshtml 中的模型声明?删除该模型声明,您将被设置

标签: c# asp.net .net asp.net-mvc


【解决方案1】:

由于_Layout.cshtml 文件中的 Vidly.ViewModels.RandomMovieViewModel 模型声明而出现此错误。

在布局视图中声明模型意味着所有使用布局视图的视图必须使用该模型类或派生自该布局视图模型类的类

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    • 2013-08-14
    • 2013-01-03
    • 2016-09-19
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    相关资源
    最近更新 更多