ASP.NET view exception
Function Edit(ByVal id As Integer) As ActionResult
Dim entities As New Deployment_devEntities()
Dim w As System.Data.Objects.ObjectQuery(Of tblWebsites) = entities.CreateQuery(Of tblWebsites)("SELECT VALUE m FROM tblWebsites as m WHERE m.WebsiteID = " & id)
Dim q = From m In entities.tblWebsites Where m.WebsiteID = id Select m
Return View(q.Single())
End Function
I have a strong typed view of type EntityFrameworkTest.tblWebsites when I pass in the results using either Linq to En开发者_运维问答tity or Entity SQL I get this error:
The model item passed into the dictionary is of type 'System.Data.Objects.ObjectQuery`1[EntityFrameworkTest.tblWebsites]',
but this dictionary requires a model item of type 'EntityFrameworkTest.tblWebsites'.
What am I doing wrong?
The code is example only.
Either your view needs to inherit from IEnumerable<tblWebsites>
or you need to change your controller to return a single instance of tblWebsites
. You can do this with the LINQ .FirstOrDefault()
method.
I don't normally do VB if I can avoid it, it looks to me like your code is inferring the type (and coming up with the assumption that it's an objectquery). Try calling .Single(Of tblWebsites) instead of just Single(). Or casting.
Your action is returning a list, but your view expects 1 object. Figure out which one of these is wrong and fix it.
The problem turned out to be that the .Single() is not supported by LINQ to Entities. Using .FirstOrDefault() or .First() worked fine.
Thank you all.
精彩评论