views:

12

answers:

2

Strange typecasting error: System.string -> weekmenu.SimpleTable

Controller

Function Index() As ActionResult

    ViewData("ListFrontName") = WeekMenuRepository.ListFrontName()
    ViewData("ListLastName") = WeekMenuRepository.ListLastName()
    Return View()

End Function

WeekMenuRepository

Public Function ListFrontName() Implements IWeekMenuRepository.ListFrontName

    Dim jow = From p In SimpleTable.SimpleTable Select p
    Return jow

End Function

View

<%  For Each item As SimpleTable In ViewData("ListFrontName")%>

<p><%=item.SimpleName%></p>

<%Next%>

When i run this i get things to work as expected (i get a list of frontnames)

However if i change this in WeekMenuRepository i get an typecasting error, why is this, what am i doing wrong and how can i fix it?

WeekMenuRepository (changed)

=> Added: select p.SimpleName

Public Function ListFrontName() Implements IWeekMenuRepository.ListFrontName

    Dim jow = From p In SimpleTable.SimpleTable Select p.SimpleName
    Return jow

End Function
A: 

After your change, you're selecting p.SimpleName explicitly instead of the entire object. You need to modify your view as follows:

<p><%= item %></p>
Justin Niessner
Damn what a stupid mistake. I'm new to OO, asp and asp mvc :p, sorry guys
kevinius
No problem...it's what we're here for.
Justin Niessner
A: 

Because in the second case ViewData("ListFrontName") is no longer IEnumerable<WhateverTheTypeOfSimpleTable> but IEnumerable<string>

To fix this change your loop in the view:

<%  For Each item As String In ViewData("ListFrontName")%>
    <p><%=item%></p>
<%Next%>
Darin Dimitrov