Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我会得到这个;“无效操作例外”;当我尝试渲染此.NET视图时?_C#_Asp.net_.net_Asp.net Mvc - Fatal编程技术网

C# 为什么我会得到这个;“无效操作例外”;当我尝试渲染此.NET视图时?

C# 为什么我会得到这个;“无效操作例外”;当我尝试渲染此.NET视图时?,c#,asp.net,.net,asp.net-mvc,C#,Asp.net,.net,Asp.net Mvc,我是.NET和C的新手(我来自Java和Spring框架),在学习教程后遇到了一些问题 我有一个简单的控制器类: namespace Vidly.Controllers { public class CustomersController : Controller { public ViewResult Index() { var customers = GetCustomers(); return

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

我有一个简单的控制器类:

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" }
            };
        }
    }
}
因此,此方法处理HTTP类型为GET的URL请求,如:

localhost:62144/Customers/Details/1
这似乎是可行的,因为我在输出控制台中获得了into Details()日志。另外,另一个日志说明客户模型对象已正确初始化,事实上我获得了以下控制台输出:

customer: 1 John Smith
然后,控制器打开包含上一个模型对象的视图结果对象(调用视图方法)

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

@model Vidly.Models.Customer

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

<h2>@Model.Name</h2>
为什么??这意味着什么

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


有什么问题?我错过了什么?如何解决此问题?

出现此错误的原因是
\u Layout.cshtml
文件中的Vidly.ViewModels.randomMoviewModel模型声明


在布局视图内声明模型意味着所有使用布局视图的视图都必须使用该模型类或从该布局视图模型类派生的类,因为作为参数传递的类型是错误的(派生的)。通常,您会尝试选择函数参数,以便只提供正确的类型。这样,编译器就可以在你点击“compile”之前为你做检查。但这并不总是可能的。如果这是不可能的,你必须做一些
is
检查你得到了什么。如果你不喜欢,你必须抛出一个例外。@Christopher mmm我怀疑我没有理解你的意思…@AndreaNobili是你的
详细信息。cshtml
文件位于
视图/Customers
目录中?MVC引擎似乎拾取了错误的视图文件。@AndreaNobili您的_Layout.cshtml是否渲染需要
随机电影视图模型的电影视图?或者如果您的详细信息视图部分呈现了电影视图?@AndreaNobili,这可能就是原因。您需要在_Layout.cshtml中声明模型吗?删除该模型声明,您将被设置为
@model Vidly.Models.Customer

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

<h2>@Model.Name</h2>
[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'.]