So I have the following ASP.NET code. Essentially, I'm trying to display an "exam" object. The exam consists of a few strings, then a nested collection of Questions. Questions have a nested collection of answers. When I run it, I receive an error that says: Unable to cast object of type 'System.Char' to type 'PracticeNet.DataEntities.Question'.
System.InvalidCastException: Unable to cast object of type 'System.Char' to type 'PracticeNet.DataEntities.Question'.
Line 15: <%#:Item.QuestionTitle %>
I have no idea why I'm receiving this error. I don't have any char types anywhere near the Question class. My only guess is that I'm somehow fundamentally misunderstanding the nested ListView object model. Can anyone shed any insight on this? Full Code is below.
<asp:Content ID="Content2" ContentPlaceHolderID="FeaturedContent" runat="server">
<asp:FormView ID="examDetails" runat="server" ItemType="PracticeNet.DataEntities.Exam" SelectMethod="GetExam" RenderOuterTable="false">
<ItemTemplate>
<div>
<h1><%#:Item.ExamTitle %></h1>
<h2><%#:Item.Description %></h2>
</div>
<br />
<asp:ListView ID="examQuestions" runat="server" ItemType="PracticeNet.DataEntities.Question" DataSource="<%#:Item.Questions %>">
<ItemTemplate>
<div>
<h1><%#:Item.QuestionTitle %></h1>
<h2><%#:Item.QuestionText %></h2>
</div>
<br />
<asp:ListView ID="questionAnswers" runat="server" ItemType="PracticeNet.DataEntities.Answer" DataSource="<%#:Item.AnswerOptions %>">
<ItemTemplate>
<div>
<h1><%#:Item.AnswerText %></h1>
</div>
</ItemTemplate>
</asp:ListView>
</ItemTemplate>
</asp:ListView>
</ItemTemplate>
</asp:FormView>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
</asp:Content>
Exam Class:
public class Exam
{
[Required]
public Guid ExamId
{
get;
set;
}
[Required, StringLength(512),Display(Name="Title")]
public string ExamTitle
{
get;
set;
}
[Required, StringLength(1024),Display(Name="Details")]
public string Description
{
get;
set;
}
[Required]
public virtual ICollection<Question> Questions
{
get;
set;
}
}
Question Class:
public class Question
{
public Question()
{
QuestionId = Guid.NewGuid();
AnswerOptions = new List<Answer>();
}
[Key]
public Guid QuestionId
{
get;
set;
}
[Required, StringLength(120), Display(Name="Title")]
public string QuestionTitle
{
get;
set;
}
[Required,StringLength(255), Display(Name="Text")]
public string QuestionText
{
get;
set;
}
[Required]
public virtual ICollection<Answer> AnswerOptions
{
get;
set;
}
}
And finally, Answer Class:
public class Answer
{
public Answer()
{
AnswerId = Guid.NewGuid();
}
[Key]
public Guid AnswerId
{
get;
set;
}
[Required, StringLength(250), Display(Name="Answer")]
public string AnswerText
{
get;
set;
}
}
DataSourcefield. It should be<%# Item.AnswerOptions %>you have:in there which is unnecessary.<%#:with<%#that looks suspect?