简体   繁体   中英

ASP.NET MVC WEB API

New to MVC. I did the tutorial @ [ http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/build-a-single-page-application-(spa)-with-aspnet-web-api-and-angularjs] and from this you produce a question and answer website. If I wanted to maintain progress ie keep a count of the number of questions correctly answered, do I need to calculate this value from retrieving the db.TriviaAnswers object or do I need to add a Count property to the TriviaAnswer class or do I need a separate variable then how do I maintain state between requests? Like ViewBag is not available in the

public async Task<IHttpActionResult> Post(TriviaAnswer answer){...}

method.

OPTION 1 as suggested below:

namespace GeekQuiz.Models
{
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using Newtonsoft.Json;

    public class TriviaResults
    {
        [Required, Key, Column(Order=1)]
        public string UserId { get; set; }

        [Required, Key, Column(Order=0)]
        public virtual int QuestionId { get; set; }
    }
}

This code throws an InvalidOperationException in the method:

private async Task<TriviaQuestion> NextQuestionAsync(string userId)

on the first line of code.

lastQuestionId = ...

I went over this tutorial a few months ago.

option 1: If you want to track progress I assume you mean progress per user, then I would advice you to add a table to the db which states saves the users ids and the ids of questions which were correctly answered - that's in case you want to save this as a persistent data and per user.

option 2: If you want the same thing, save the data per user but only for this session, you can save the data in the session variable as a dictionary<userid, list<questionid>> .

One thing you should notice is that those question repeat in an endless loop, so you might want to change that.

In both options when you need to know the count u can just go to the table or dictionary and get the number of correct answers.

I hope that answers your question.

To use the session var:

Session["name"] = value;
Session.Remove("name");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM