Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.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# 传递给视图的错误模型项的类型为';x';,但本词典需要类型为';y';_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 传递给视图的错误模型项的类型为';x';,但本词典需要类型为';y';

C# 传递给视图的错误模型项的类型为';x';,但本词典需要类型为';y';,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我正在学习表单,我不明白我的代码有什么问题。我通过CustomerIdviewModel查看,但它需要随机电影视图模型 错误屏幕截图: 这是我的密码 您还可以检查我的项目的全部代码: 客户控制器 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Vidly.Models; using Vidly.ModelsView;

我正在学习表单,我不明白我的代码有什么问题。我通过CustomerIdviewModel查看
,但它需要
随机电影视图模型

错误屏幕截图:

这是我的密码

您还可以检查我的项目的全部代码:

客户控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Vidly.Models;
using Vidly.ModelsView;
namespace Vidly.Controllers
{
    public class CustomersController : Controller
    {
        // GET: Customers
        public ActionResult Index()
        {
                var customers = new List<Customer>
                 {
                    new Customer { Id = 1, Name= "Koksztul"},
                    new Customer { Id = 2, Name= "Januszek"}
                 };

                var viewModel = new ByCustomerIdViewModel
                {
                    Customers1 = customers
                };
                return View(viewModel);
        }

        [Route("customers/details/{id}")]
        public ActionResult ByCustomerId(int id)
        {
            var customers = new List<Customer>
                 {
                    new Customer { Id = 1, Name= "Koksztul"},
                    new Customer { Id = 2, Name= "Januszek"}
                 };

            var viewModel = new ByCustomerIdViewModel
            {
                customerId = id,
                Customers1 = customers
            };
            return View(viewModel);
        }
    }
}
ByCustomerIdModelView

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Vidly.Models;

namespace Vidly.ModelsView
{
    public class ByCustomerIdViewModel
    {
        public int customerId { get; set; }
        public List<Customer> Customers1 { get; set; }
    }
}
@model Vidly.ModelsView.ByCustomerIdViewModel
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}


@foreach (var customer in Model.Customers1)
{
<li> @Html.ActionLink(customer.Name, "details", "customers", new { id = customer.Id},
                null  
                )</li>

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用Vidly.模型;
命名空间Vidly.ModelsView
{
按CustomerIdviewModel的公共类
{
public int customerId{get;set;}
公共列表客户1{get;set;}
}
}
查看

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Vidly.Models;

namespace Vidly.ModelsView
{
    public class ByCustomerIdViewModel
    {
        public int customerId { get; set; }
        public List<Customer> Customers1 { get; set; }
    }
}
@model Vidly.ModelsView.ByCustomerIdViewModel
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}


@foreach (var customer in Model.Customers1)
{
<li> @Html.ActionLink(customer.Name, "details", "customers", new { id = customer.Id},
                null  
                )</li>

}
@model Vidly.ModelsView.ByCustomerIdViewModel
@{
ViewBag.Title=“Index”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
@foreach(Model.Customers1中的var客户)
{
  • @Html.ActionLink(customer.Name,“details”,“customers”,new{id=customer.id}, 无效的 )
  • }
    您是否希望两种操作方法返回相同的视图?除非另行指定,否则操作方法将查找具有相同名称的视图,因此索引操作方法将查找名为Index的视图。如果这是你想要的,有办法覆盖它。你会得到什么错误?你能加个截图什么的吗?欢迎加入。从您包含的代码中,我无法了解问题所在。您是否面临编译问题?如果是,请准确指定在哪一点,并添加错误消息。例如,在Views/Customers中定义的两个视图都将Vidly.ModelsView.ByCustomerIdViewModel作为模型
    。此外,从您共享的控制器来看,似乎您提供了此功能。所以我不明白有什么问题。谢谢,您可能只需要重建项目或其他内容。@GidiBloke屏幕截图是否希望两种操作方法返回相同的视图?除非另行指定,否则操作方法将查找具有相同名称的视图,因此索引操作方法将查找名为Index的视图。如果这是你想要的,有办法覆盖它。你会得到什么错误?你能加个截图什么的吗?欢迎加入。从您包含的代码中,我无法了解问题所在。您是否面临编译问题?如果是,请准确指定在哪一点,并添加错误消息。例如,在Views/Customers中定义的两个视图都将Vidly.ModelsView.ByCustomerIdViewModel作为模型
    。此外,从您共享的控制器来看,似乎您提供了此功能。所以我不明白有什么问题。谢谢,您可能只需要重建您的项目或其他东西。@GidiBloke屏幕截图