传递到字典中的模型项是类型的,但是这个字典需要类型为'System.Collections.Generic.IEnumerable`1 []'的模型项。

时间:2016-06-19 23:53:34

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

执行应用程序后,我得到了

  

传递到字典中的模型项的类型为'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>

DbController.cs

public class DbController : Controller
{ 
    public ActionResult Index()
    {
        ServiceReference1.ServiceClient obj = new ServiceReference1.ServiceClient();
        return View(obj.GetStudents()); 
    } 
} 

我的iService.cs

// 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();

1 个答案:

答案 0 :(得分:1)

尝试以下更改。

public ActionResult Index()
  { 
     ServiceReference1.ServiceClient obj = new ServiceReference1.ServiceClient();
     return View(obj.GetStudents().ToList()); 
  }

按如下方式更改视图。

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

修改

@model IEnumerable<MvcWcfApplication.ServiceReference1.StudentDetail>

展示学生

您需要在StudentDetail类中打印学生属性的值 类似&#34; item.StudentName&#34;。 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>