Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.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# 传递到字典中的模型项的类型为,但此字典需要类型为';System.Collections.Generic.IEnumerable`1[]和#x27;_C#_Asp.net Mvc_Asp.net Mvc 4_Generics - Fatal编程技术网

C# 传递到字典中的模型项的类型为,但此字典需要类型为';System.Collections.Generic.IEnumerable`1[]和#x27;

C# 传递到字典中的模型项的类型为,但此字典需要类型为';System.Collections.Generic.IEnumerable`1[]和#x27;,c#,asp.net-mvc,asp.net-mvc-4,generics,C#,Asp.net Mvc,Asp.net Mvc 4,Generics,执行应用程序后,我得到 传入字典的模型项的类型为“MvcWcfApplication.ServiceReference1.StudentDetail[]”,但此字典需要类型为“System.Collections.Generic.IEnumerable`1[MvcWcfApplication.ServiceReference1.ServiceClient]的模型项 鉴于 @model IEnumerable<MvcWcfApplication.ServiceReference1.Servi

执行应用程序后,我得到

传入字典的模型项的类型为“MvcWcfApplication.ServiceReference1.StudentDetail[]”,但此字典需要类型为“System.Collections.Generic.IEnumerable`1[MvcWcfApplication.ServiceReference1.ServiceClient]的模型项

鉴于

@model IEnumerable<MvcWcfApplication.ServiceReference1.ServiceClient>
....
<table>
    <tr>
        <th></th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
            </td>
        </tr>
    }
</table>
我的副手

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService" in both code and config file together.
[ServiceContract]
public interface IService
{
    [OperationContract]
    List<StudentDetail> GetStudents();
//注意:您可以使用“重构”菜单上的“重命名”命令同时更改代码和配置文件中的接口名称“IService”。
[服务合同]
公共接口设备
{
[经营合同]
列出GetStudents();

尝试以下更改

public ActionResult Index()
  { 
     ServiceReference1.ServiceClient obj = new ServiceReference1.ServiceClient();
     return View(obj.GetStudents().ToList()); 
  }
按如下所示更改视图

@model IEnumerable<MvcWcfApplication.ServiceReference1.ServiceClient.StudentDetail>
@model IEnumerable
编辑

@model IEnumerable<MvcWcfApplication.ServiceReference1.StudentDetail>
@model IEnumerable
显示学生

您需要在StudentDetail类中打印学生属性的值 类似于“item.StudentName”的内容。StudentName是类中的一个属性

<table>
    <tr>
        <th></th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td> item.StudentName </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
            </td>
        </tr>
    }
</table>

@foreach(模型中的var项目)
{
item.StudentName
@ActionLink(“编辑”,“编辑”,新的{/*id=item.PrimaryKey*/})|
@ActionLink(“详细信息”,“详细信息”,新的{/*id=item.PrimaryKey*/})|
@ActionLink(“删除”,“删除”,新的{/*id=item.PrimaryKey*/})
}

错误消息是不言自明的。您调用一个返回
列表的服务,并将其传递给一个视图,该视图期望
IEnumerable
-
StudentDetail
不是
ServiceClient
尝试将“IEnumerable”更改为“IEnumerable”新的错误编译器错误消息:CS0426:类型名“StudentDetail”不存在在“MvcWcfApplication.ServiceReference1.ServiceClient”类型中,我尝试了该操作,但它无法识别我的“StudentDetail”;给了我一条曲线。@Ollivander尝试再次关闭和打开视图。Intellisense在视图上导入名称空间时并不总是起作用。它应该编译并运行。新错误:编译器错误消息:CS0426:类型“MvcWcfApplication.ServiceReference1.ServiceClient”中不存在类型名称“StudentDetail”。谢谢,这行得通。我正在做一个使用web服务与数据库交互的教程。但是,使用编辑后,它不会按预期显示数据库中的学生。它只显示一个空列表,带有“创建”、“编辑”、“详细信息”和“删除”.我是新使用mvc的,但它不应该显示我的listHi,所以我的视图可以工作。我正在尝试在我的应用程序中对学生记录进行CRUD操作。你能帮我处理这些代码吗?
<table>
    <tr>
        <th></th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td> item.StudentName </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
            </td>
        </tr>
    }
</table>