0

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 i.e. 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 = ...
Glen
  • 802
  • 1
  • 11
  • 27

1 Answers1

1

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");
LiranBo
  • 2,054
  • 2
  • 23
  • 39
  • It does answer my question. I do not mind either option as both have there purposes. There is case for option 1 where I need it persisted if I as in the user (or anyone else for that matter) need to see the results at a later stage and option2 when the results do not count for anything and thus only matter to the current user at the time. The only reason I am sceptical of option 2 because I just need to know if I log out and log in again does my session get reset. Do I need to add call to clear the Session in the Account Controller Login method? – Glen Oct 24 '15 at 07:41
  • Am I missing something here, Session is not available in the context I described, so how/where do I call it? – Glen Oct 24 '15 at 07:46
  • I'm not 100% sure when it comes to login\logout, you can simply check it yourself. and about adding a call to clear the session, well that's easy as placing the values there in the first place. I will edit my answer with the code – LiranBo Oct 24 '15 at 07:47
  • Did you see my comment I added just before yours, where do I call it as the Session is not available. It says Session is not available in the current context. NB: I do not need a lesson on how to set/get/remove session. – Glen Oct 24 '15 at 07:51
  • try this: [link](http://stackoverflow.com/questions/9594229/accessing-session-using-asp-net-web-api) – LiranBo Oct 24 '15 at 07:51
  • I tried option 1 and whatever I try it throws an InvalidOperation Exception. Can you tell me how I should declare my class? Unable to determine composite primary key ordering for type 'GeekQuiz.Models.TriviaResults'. Use the ColumnAttribute (see http://go.microsoft.com/fwlink/?LinkId=386388) or the HasKey method (see http://go.microsoft.com/fwlink/?LinkId=386387) to specify an order for composite primary keys. I do not understand this. – Glen Oct 24 '15 at 12:34
  • can you update your model into your question? basically you need to use `[Key, Column(Order = 0)] [Key, Column(Order = 1)]` in-order define the order of the keys. – LiranBo Oct 24 '15 at 12:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93244/discussion-between-glen-and-liranbo). – Glen Oct 24 '15 at 12:42