开发者

Should i use threads when executing action method through AJAX?

I am building a questionnarie. When a user clicks on an answer possibility for a multiple choice question (this is a radio button), i call an action method to save this answer.

The code:

<script language="javascript">

$(document).ready(function () {

    $('.MCQRadio').click(function () {

        var question_id = $(this).attr('question-id');
        var mcq_id = $(this).attr('mcq-id');

        $.ajax({
            url: '/SaveSurveyAnswers/SaveMCQAnswer',
            data: { "mcq_id": mcq_id, "question_id": question_id },
            success: function (data) {
            }
        });
    });
});

The code to save the answer:

public EmptyResult SaveMCQAnswer(int mcq_id, int question_id)
    {
        MCQ_Answers mcqa = null;
        try
        {
            mcqa = db.MCQ_Answers.Single(x => x.question_ID == question_id);
        }
        catch (InvalidOperationException e)
        {

        }
        if (mcqa != null)
        {
            mcqa.mcq_id = mcq_id;
        }
        else
        {
            MCQ_Answers mcq_answer = new MCQ_Answers()
            {
                question_ID = question_id,
                respondent_id = 1
            };

            db.MCQ_Answers.AddObject(mcq_answer);
        }
        db.SaveChanges();
        return new EmptyResult();
    }

If a question has 5 answer possibilities, and i click on them randomly and fast, and then go back to the previous pa开发者_StackOverflowge, ie, when i return the correct answer wont be saved. Should i use threading to make sure the correct answer is saved? And how?

Thanks


rather than saving your answer by post all the time, you can just create a JSOn object and save the answers within json. you can then at the end post all completed answers in one go.

take a look at this: http://msdn.microsoft.com/en-us/scriptjunkie/ff962533

basically this will allow you to store session data - json on the remote machine you then just need an add, delete function and away you go....

i use this to huge extent in an application that would require the server to be updated with the location of objects on a canvas, however with sessvars i just keep all the X and Y locations within there and do a final push of JSON when i am done.

if you change pages, you can then get your values from the JSON object without a server call.

as a note you may also be better off with tabs or hiden sections of form, and therfor reduce the need to re-populate say page1, page2 etc as they will already be there, just hidden!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜