使用 ASP.NET / Linq 进行在线测试
正在进行在线测试。
我有 3 个表
- 问题
- 主题
- 主题
我制作了一个存储过程,它返回 25 条随机记录。我想将其存储在内存中,然后使用 AJAX 一次显示 1 个问题。我不想访问数据库 25 次,因为有很多用户,我尝试将结果存储在视图状态中,但后来我无法将其转换回来。如果我使用
Dim qus = from viewstate("questions")
它可以工作,但是当我一次检索 1 条记录时它不起作用。
代码:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
ViewState.Add("QuestionNo", 0)
Dim qus = From q In PML.PM_SelectRandomQuestionFM Select q
viewstate.add("questions",qus)
LoadQuestion(0)
End If
End Sub
Private Sub LoadQuestion(ByVal i As Integer)
Dim QuestionNo As Integer = CType(ViewState("QuestionNo"), Integer) + 1
Try
If QuestionNo <= 25 Then
Dim qus = viewstate("questions")
Me._subjectTopic.Text = String.Format("<b>Subject:</b> {0} -- <b>Topic:</b> {1}", qus(i).subjectName, qus(i).TopicName)
Me._question.Text = " " & qus(i).Question
Me._answer1.Text = " " & qus(i).Answer1
Me._answer2.Text = " " & qus(i).Answer2
Me._answer3.Text = " " & qus(i).Answer3
Me._answer4.Text = " " & qus(i).Answer4
Me._questionNo.Text = String.Format("Question No. {0} / 25", QuestionNo)
ViewState.Add("QuestionNo", QuestionNo)
Else
Server.Transfer("freeMemberResult.aspx")
End If
Catch ex As Exception
Throw New System.Exception(ex.ToString)
End Try
End Sub
我尝试将对象转换为
Dim qus = CType(ViewState("questions"), IQueryable(Of PM_SelectRandomQuestionFMResult))
但随后出现此错误
System.Linq.Enumerable+WhereSelectEnumerableIterator`2
如果我的在线测试方法错误,请帮助或者是否有其他方法可以做到这一点。
问候
Working on a Online Test.
I have 3 tables
- Questions
- Subject
- Topic
I have made a stored procedure which returns 25 random records. I want to store it in-memory and then display 1 question at a time with AJAX. I don't want to hit database 25 times as there are many users, I tried and store the result in viewstate but then I am not able to cast it back. if I use
Dim qus = from viewstate("questions")
it works, but it doesn't work when I retrieve 1 record at a time.
Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
ViewState.Add("QuestionNo", 0)
Dim qus = From q In PML.PM_SelectRandomQuestionFM Select q
viewstate.add("questions",qus)
LoadQuestion(0)
End If
End Sub
Private Sub LoadQuestion(ByVal i As Integer)
Dim QuestionNo As Integer = CType(ViewState("QuestionNo"), Integer) + 1
Try
If QuestionNo <= 25 Then
Dim qus = viewstate("questions")
Me._subjectTopic.Text = String.Format("<b>Subject:</b> {0} -- <b>Topic:</b> {1}", qus(i).subjectName, qus(i).TopicName)
Me._question.Text = " " & qus(i).Question
Me._answer1.Text = " " & qus(i).Answer1
Me._answer2.Text = " " & qus(i).Answer2
Me._answer3.Text = " " & qus(i).Answer3
Me._answer4.Text = " " & qus(i).Answer4
Me._questionNo.Text = String.Format("Question No. {0} / 25", QuestionNo)
ViewState.Add("QuestionNo", QuestionNo)
Else
Server.Transfer("freeMemberResult.aspx")
End If
Catch ex As Exception
Throw New System.Exception(ex.ToString)
End Try
End Sub
I tried casting the object to
Dim qus = CType(ViewState("questions"), IQueryable(Of PM_SelectRandomQuestionFMResult))
but then I get this error
System.Linq.Enumerable+WhereSelectEnumerableIterator`2
Please HELP or if there is any other method to do it, if my method of doing online test is wrong.
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
IMO,你对此过度设计了。为什么要把数据保存在内存中呢?为什么不将每个问题写入一个 div,然后隐藏除“当前问题”之外的所有问题 div。
实现起来更容易,并且您不需要通过多个 AJAX 调用来访问服务器,这也使得保存状态(之前回答的问题等)变得非常非常容易。
IMO, you're over-engineering this. Why screw around trying to hold the data in memory? Why not write each question to a div, and then hide all of the question divs except for the "current question".
Much easier to implement and you're not hitting the server with several AJAX calls, This also make saving state (previously answered questions, etc) much, much easier.
您是否尝试过仅使用 Session 来维护状态?是否存在禁止您这样做的要求?
Have you tried just using Session to maintain state? Is there a requirement that prohibits you from doing this?