开发者

ASP.NET MVC: Strange typecasing error

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


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%>


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜