john.gale.92102 0 Newbie Poster

Hi,
I am accessing a stored procedure through the Entity Framework. I have generated the access code, as well as the view. The problem is that the procedure returns an ObjectResult(Of T), and the view is expeting IEnumerable. I have tried various ways of casting the ObjectResult, but I am still getting and error indicating that it is not changing. If I do not attempt to cast item I pass to the view, I get the error:

The model item passed into the dictionary is of type 'System.Data.Objects.ObjectResult1[gbip_new.FirmsNamed_Result]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[gbip_new.Firm]'.

I have tried changing the Model in the view, but that seems to cause other problems.

Here is the function from my controller

            Function FirmList(Optional ByVal firmName As String = "") As ActionResult
                Dim firms As IEnumerable(Of Global.gbip_new.Firm)
                Dim firmResult As ObjectResult(Of Global.gbip_new.FirmsNamed_Result)
                firmResult = db.FirmsNamed(firmName)
                firms = firmResult.AsEnumerable
                Return View(firms)
            End Function

Here is the error message

Message=Unable to cast object of type 'System.Data.Objects.ObjectResult1[gbip_new.FirmsNamed_Result]' to type 'System.Collections.Generic.IEnumerable1[gbip_new.Firm]'.

The View code is:

@ModelType IEnumerable(Of gbip_new.Firm)

@Code
    ViewData("Title") = "FirmList"
End Code

<h2>FirmList</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(Function(model) model.shortName)
        </th>
        <th>
            @Html.DisplayNameFor(Function(model) model.name)
        </th>

        <th></th>
    </tr>

@For Each item In Model
    Dim currentItem = item
    @<tr>

        <td>
            @Html.DisplayFor(Function(modelItem) currentItem.shortName)
        </td>
        <td>
            @Html.DisplayFor(Function(modelItem) currentItem.name)
        </td>

        <td>
            @Html.ActionLink("Quotes", "Edit", New With {.id = currentItem.id}) |
            @Html.ActionLink("Edit", "Edit", New With {.id = currentItem.id}) |
            @Html.ActionLink("Details", "Details", New With {.id = currentItem.id}) |
            @Html.ActionLink("Delete", "Delete", New With {.id = currentItem.id})
        </td>
    </tr>
Next

</table>

The class defination for the firm is:

'------------------------------------------------------------------------------
' <auto-generated>
'    This code was generated from a template.
'
'    Manual changes to this file may cause unexpected behavior in your application.
'    Manual changes to this file will be overwritten if the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------

Imports System

Partial Public Class FirmsNamed_Result
    Public Property id As Integer
    Public Property corpId As Nullable(Of Integer)
    Public Property oldId As Nullable(Of Integer)
    Public Property shortName As String
    Public Property name As String
    Public Property classId As Nullable(Of Integer)
    Public Property classGroupId As Nullable(Of Integer)
    Public Property agentId As Nullable(Of Integer)
    Public Property oldAgentId As Nullable(Of Integer)
    Public Property agencyId As Nullable(Of Integer)
    Public Property addressId As Nullable(Of Integer)
    Public Property active As String
    Public Property contact As String
    Public Property policyId As Nullable(Of Integer)
    Public Property billDivNo As String
    Public Property associationId As Nullable(Of Integer)
    Public Property binderNo As String
    Public Property effectiveDate As Nullable(Of Date)
    Public Property termDate As Nullable(Of Date)
    Public Property statusId As Integer
    Public Property state As Nullable(Of Integer)
    Public Property createdBy As Integer
    Public Property createdAt As Nullable(Of Date)
    Public Property updatedBy As Integer
    Public Property updatedAt As Nullable(Of Date)

End Class