使用 ASP.NET / Linq 进行在线测试

发布于 2024-08-05 00:29:15 字数 1914 浏览 10 评论 0原文

正在进行在线测试。

我有 3 个表

  1. 问题
  2. 主题
  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

  1. Questions
  2. Subject
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

依 靠 2024-08-12 00:29:16

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.

罪#恶を代价 2024-08-12 00:29:16

您是否尝试过仅使用 Session 来维护状态?是否存在禁止您这样做的要求?

Dim qus = CType(Me.Session("questions"), IQueryable(Of PM_SelectRandomQuestionFMResult))

Have you tried just using Session to maintain state? Is there a requirement that prohibits you from doing this?

Dim qus = CType(Me.Session("questions"), IQueryable(Of PM_SelectRandomQuestionFMResult))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文