Danny Goodall
Danny Goodall

Reputation: 838

Incrementing and Decrementing a querystring parameter?

In my current MVC project(the third question I've asked on this same project!), I have a 'Landing Page' which after one click of a button redirects to the following page URL:

localhost:111111/AccountsFinance/Index?questionId=1

I can edit the URL manually and change Id=2 which does change what is displayed, but I need a button to do this of course.

Within this View, I have the following:

@using (Html.BeginForm("Index", "AccountsFinance", FormMethod.Post))
{
    //SOME CODE

    <input class="btn btn-success" type="submit" value="Back" />
    <input class="btn btn-success" type="submit" value="Next" />
}

Here is my controller code, not sure if it is necessary for this question but better to include it than not to!

    public AccountsFinanceQuestion afqList = new AccountsFinanceQuestion();

    public ActionResult Index(string ans)
    {
        int qId = int.Parse(Request.QueryString["questionId"]);

        using (S3WEntities1 ent = new S3WEntities1())
        {
            afqList.Question = ent.Questions.Where(w => w.QuQuestionId == qId).Select(s => s.QuQuestion).FirstOrDefault().ToString();
            afqList.Answers = ent.Answers.Where(w => w.AnsQuestionId == qId).Select(s => s.AnsAnswer).ToList();
        }

        return View("Index", afqList);
    }

So how would I increment/decrement the questionId=X part of the query string so that it moves on to the next question/the previous question. This is all in the same view on the same method on the same controller. I also understand that this may not work so well for the very first question if the user clicks back, as the URL is completely different, but that is something to come on to at a later date.

Upvotes: 0

Views: 342

Answers (2)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236268

First of all, you don't need to parse query string parameters manually - default binder will do it for you. Just define method parameter with corresponding name. E.g.

public ActionResult Index(int questionId)

Second, seems like you are reviewing details of some question. So I don't think that Index is appropriate action for that. I suggest to use Details or Edit:

public ActionResult Details(int questionId)

And regarding links to next and previous questions - you can just generate action links in your view:

@Html.ActionLink("Back", "Details", new { questionId = Model.QuQuestionId - 1 }
@Html.ActionLink("Next", "Details", new { questionId = Model.QuQuestionId + 1 })

Also note, that your approach has several problems:

  • What if some question was deleted and ids are not incremental anymore?
  • What if current question has id = 1, i.e. it's a first question? It should not have link to previous question
  • What if current questions is last one?

Easiest way to solve these problems - pass ids of previous and next questions to your view. You can use model properties for that, or you can use ViewBag:

ViewBag.PrevQuestionId = ent.Questions
    .Where(q => q.QuQuestionId < questionId)
    .OrderByDescending(q => q.QuQuestionId)
    .Select(q => q.QuQuestionId)
    .FirstOrDefault();

Then in View you can conditionally add link to previous question:

 @if (ViewBag.PrevQuestionId > 0) {
      @Html.ActionLink("Back", "Details", new { questionId = ViewBag.PrevQuestionId }
 }

Same with next question

Upvotes: 2

Vnuuk
Vnuuk

Reputation: 6527

You can do something like this in your controller:

int qId = int.Parse(Request.QueryString["questionId"]);
Response.RedirectToRoute("current-route?questionId=" + qId++);

Upvotes: -1

Related Questions