Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.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# MVC-传递到字典中的模型项的类型为,但此字典需要类型为';System.Collections.Generic.List`1_C#_Asp.net Mvc_Model View Controller - Fatal编程技术网

C# MVC-传递到字典中的模型项的类型为,但此字典需要类型为';System.Collections.Generic.List`1

C# MVC-传递到字典中的模型项的类型为,但此字典需要类型为';System.Collections.Generic.List`1,c#,asp.net-mvc,model-view-controller,C#,Asp.net Mvc,Model View Controller,我无法理解为什么会出现这个错误,因为我尝试做的应该相当简单: 传递到字典中的模型项的类型为 “WebApplication1.Models.Location”,但此词典需要 类型的模型项 'System.Collections.Generic.List'1[WebApplication1.Models.Location]' 查看代码位置\u List.cshtml @model List<WebApplication1.Models.Location> @{ Layout

我无法理解为什么会出现这个错误,因为我尝试做的应该相当简单:

传递到字典中的模型项的类型为 “WebApplication1.Models.Location”,但此词典需要 类型的模型项 'System.Collections.Generic.List'1[WebApplication1.Models.Location]'

查看代码位置\u List.cshtml

@model List<WebApplication1.Models.Location>

@{
    Layout = "~/Views/Shared/_LayoutAdminPanel.cshtml";
}

<div class="table-responsive">
    <table class="table">
        <thead class="thead-inverse">
            <tr>
                <th>Location ID</th>
                <th>Location Name</th>
                <th>Photo Path</th>
                <th>Details</th>
                <th>City ID</th>
                <th>Modify</th>
            </tr>
        </thead>
        <tbody>
            @for (int i = 0; i < Model.Count; i++)
            {

                    <tr>
                        <th scope="row">Location00_@Model[i].LocationID</th>
                        <td>@Model[i].LocationName</td>
                        <td>@Model[i].PhotoPath</td>
                        <td>@Model[i].Details</td>
                        <td>@Model[i].CityID</td>
                        <td><a href="/Admin/Location_Edit?ID=@i">Edit</a></td>
                    </tr>

            }
@型号列表
@{
Layout=“~/Views/Shared/\u LayoutAdminPanel.cshtml”;
}
位置ID
地点名称
光路
细节
城市标识
修改
@for(int i=0;i
控制器代码AdminController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models;

namespace MySafar.Controllers
{
    public class AdminController : Controller
    {

      public ActionResult Location_New()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Location_List()
        {
            Location l = new Location();
            l.LocationID = Convert.ToInt32(Request.Form["LocationID"]);
            l.LocationName = Request.Form["LocationName"];
            l.PhotoPath = Request.Form["PhotoPath"];
            l.Details = Request.Form["Details"];
            l.CityID = Convert.ToInt32(Request.Form["CityID"]);

            if (Session["Location"]==null)
            {
                Session["Location"] = new List<Location>();
            }

            List<Location> loc = (List<Location>)Session["Location"];
            loc.Add(l);
            return View("Location_List", l);
        }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
使用WebApplication1.模型;
命名空间MySafar.Controllers
{
公共类AdminController:Controller
{
公共行动结果位置_New()
{
返回视图();
}
[HttpPost]
公共行动结果位置_列表()
{
位置l=新位置();
l、 LocationID=Convert.ToInt32(Request.Form[“LocationID]”);
l、 LocationName=Request.Form[“LocationName”];
l、 PhotoPath=请求。表单[“PhotoPath”];
l、 详细信息=请求。表格[“详细信息”];
l、 CityID=Convert.ToInt32(Request.Form[“CityID”]);
if(会话[“位置”]==null)
{
会话[“位置”]=新列表();
}
List loc=(List)会话[“位置”];
loc.添加(l);
返回视图(“位置列表”,l);
}
}
我检查了整个代码4-5次,我不明白实际问题在哪里。。。
有什么解决方案吗?请……

根据您的错误,听起来好像您在cshtml文件中指定的任何模型都需要一个WebApplication1.Models.Location类型列表,但您只向其中传递了一个WebApplication1.Models.Location,这一点很清楚

return View("Location_List", l);
或者将cshtml文件所期望的模型更改为单个位置,或者将位置列表传递给模型

return View("Location_List", loc);

将解决您的问题。干杯!

根据您的错误,听起来您在cshtml文件中指定的任何模型都需要一个WebApplication1.Models.Location类型列表,但您只向其中传递了一个WebApplication1.Models.Location,这一点很清楚

return View("Location_List", l);
或者将cshtml文件所期望的模型更改为单个位置,或者将位置列表传递给模型

return View("Location_List", loc);

将解决您的问题。干杯!

您尝试将
l
变量发送到
位置列表
视图:

return View("Location_List", l);

l
Location
类型的一个实例。但是在Location\u List.cshtml文件中,您定义模型类型为
List
。我的意思是您需要将
loc
变量发送到视图,而不是将
l
变量发送到
Location\u列表
视图:

return View("Location_List", l);

l
Location
类型的一个实例。但是在Location\u List.cshtml文件中,您定义模型类型为
List
。我的意思是您需要将
loc
变量发送到视图,而不是
l

看起来只是简单的键入错误-
返回视图(“Location\u List”,loc)
。。。(loc而不是l)另外,请确保阅读指南,不要发布大量不必要的代码来演示问题。看起来只是简单的键入错误-
返回视图(“位置列表”,loc);
…(loc而不是l)另外,请确保阅读指南,不要发布大量不需要演示问题的代码。