Solved

Create wizard type navigation in MVC

Posted on 2010-09-01
8
949 Views
Last Modified: 2013-11-08
Hi
I’m writing a customer survey app in MVC.
I have a view that is populated by looping through a customer survey and outputs the questions and question choices.  For example

Q1  What would you rate as acceptable?
a.       10 hours  
b.      20 hours
c.      30 hours
Q2 What is the time scale for this issue?
a.      1 hour
b.      2 hours
c.      3 hours

And so on.    In the view all the questions are displayed in one view but I want to break up each question so it is displayed one at a time.

I want to convert the view so it is a in wizard format with a next button so that each question is displayed at one time and when you hit the “next” button the second question is displayed and so on.

For info  this is a snippet of how I loop through the customer survey to populate the view

  <%
            foreach (var qChoice in question.questionChoices)
            { %>
       ................................
0
Comment
Question by:ToString1
  • 4
  • 4
8 Comments
 
LVL 27

Accepted Solution

by:
nmarun earned 500 total points
ID: 33575201
There's no need for your to have that foreach loop in the view. Here's how I'd work this up.

Create a view that can display a question and it's choices (a foreach might be required for the choices). Now my controller will give only one question to the view. All I need to know what question was passed to the view (I'll use a hidden field on the view to store the question id).

Once the user hits the 'next' / 'back' button, I can check the formCollection["next"] or formCollection["back"] to have a value in them and decide to display the appropriate question. The 'post' version of your action method might look something like:

[HttpPost]
public ViewResult Survey(Question currentQuestion, FormCollection formCollection)
{
    // save the current question's answer
    Question questionToBeDisplayed;
    if(!string.IsNullOrEmpty(formCollection["next"])
    {
        // display the next question
        questionToBeDisplayed = GetNextQuestion(currentQuestion.Id);
    }
    else // if(!string.IsNullOrEmpty(formCollection["back"])
    {
       // display the previous question
       questionToBeDisplayed = GetPrevQuestion(currentQuestion.Id);
    }

    return View(questionToBeDisplayed);
}
0
 

Author Comment

by:ToString1
ID: 33575249
OK nmarun  an excellent answer.   I will get back to you shortly and try out your solution
0
 

Author Comment

by:ToString1
ID: 33575339

Thanks
Currently I have an controller that accepts a the whole survey answers as a list.......

public ActionResult CompletedSurvey(int? surveyID, List<surveyResponse> Responses)



Could you expand with some more code on how you would go about this please?  


0
 

Author Comment

by:ToString1
ID: 33575564
Infact the main thing I need to do is to know how to display each question in turn.   You see I have the questions stored in database table with primary key.

So when I first load the questionnaire how would I know how to display the first question?

So on first load would it be

questionToBeDisplayed = GetNextQuestion(currentQuestion.Id);  //would this be the lowest primary key?


Also are the code

if(!string.IsNullOrEmpty(formCollection["back"])

for the next and back button?

0
Network it in WD Red

There's an industry-leading WD Red drive for every compatible NAS system to help fulfill your data storage needs. With drives up to 8TB, WD Red offers a wide array of solutions for customers looking to build the biggest, best-performing NAS storage solution.  

 
LVL 27

Assisted Solution

by:nmarun
nmarun earned 500 total points
ID: 33575868
Here's a working model I have for you.

Arun

// my model class

public class Choice

{

    public int ChoiceId { get; set; }

    public string Text { get; set; }

}



public class Question

{

    public int QuestionId { get; set; }

    public string Text { get; set; }

    public List<Choice> Choices { get; set; }

}



// my controller

private List<Question> questions;

// this code is only for my mock purposes

// you will not be needing this as you'll read it from the database

public HomeController()

{

    questions = new List<Question>();

    questions.Add(new Question

                      {

                          QuestionId = 1,

                          Text = "What is 2x2?",

                          Choices = new List<Choice>

                                        {

                                            new Choice{ ChoiceId = 1, Text = "2" },

                                            new Choice{ ChoiceId = 2, Text = "4" },

                                            new Choice{ ChoiceId = 3, Text = "6" },

                                        }

                      });

    questions.Add(new Question

                      {

                          QuestionId = 2,

                          Text = "What is 26x7",

                          Choices = new List<Choice>

                                        {

                                            new Choice{ ChoiceId = 4, Text = "182" },

                                            new Choice{ ChoiceId = 5, Text = "134" },

                                            new Choice{ ChoiceId = 6, Text = "5" },

                                        }

                      });

    questions.Add(new Question

                      {

                          QuestionId = 3,

                          Text = "What is 25x24?",

                          Choices = new List<Choice>()

                                        {

                                            new Choice{ ChoiceId = 7, Text = "175" },

                                            new Choice{ ChoiceId = 8, Text = "19", },

                                            new Choice{ ChoiceId = 9, Text = "600" },

                                        }

                      });

}



public ViewResult Survey()

{

    return View(questions[0]);

}



[HttpPost]

public ViewResult Survey(int QuestionId, int? ChoiceId, FormCollection formCollection)

{

    Question questionToBeDisplayed;

    if(!string.IsNullOrEmpty(formCollection["back"]))

    {

        questionToBeDisplayed = GetPreviousQuestion(QuestionId);

    }

    else

    {

        questionToBeDisplayed = GetNextQuestion(QuestionId);

    }

    return View(questionToBeDisplayed);

}



private Question GetPreviousQuestion(int questionId)

{

    // dummy implementation

    return questions[0];

}



private Question GetNextQuestion(int questionId)

{

    // dummy implementation; code below just gets the next question from the collection

    return questions.Where(q => q.QuestionId == questionId + 1).FirstOrDefault();

}



// my view

<% using (Html.BeginForm()) {%>



    <input name="QuestionId" id="QuestionId" type="hidden" value="<%= Html.Encode(Model.QuestionId) %>" />

    

    Question: <%= Html.Encode(Model.Text) %>

    <br />

    <ul>

    <% for (int i = 0; i < Model.Choices.Count; i++) { %>

        <li>

            <%= Html.RadioButton("ChoiceId", Model.Choices[i].ChoiceId) %> &nbsp; <%= Model.Choices[i].Text %>

        </li>

    <% } %>

    </ul>

    <input type="submit" id="back" name="back" value="<< Back" /> &nbsp;&nbsp;

    <input type="submit" id="next" name="next" value="Next >>" />

 <% } %>

Open in new window

0
 
LVL 27

Expert Comment

by:nmarun
ID: 33575902
To answer your concern in post 33575339, MVC cannot create a collection the way you are expecting. It's Model Binding logic is not very mature yet. You can create your own model binding logic to parse the posted html and create the collection, but this will quite complicated for what you're trying to do. Moreover, it is not needed that you get the collection. You just need to know what choice was selected for the particular question. This is just what my code is returning to you (see the [HttpPost] version of the Survey action method).

Arun
0
 
LVL 27

Assisted Solution

by:nmarun
nmarun earned 500 total points
ID: 33575957
I'm experiencing intermittent power outage today so I'm responding in smaller parts.

Now coming to your questions in post 33575564.

>> So when I first load the questionnaire how would I know how to display the first question?
This would be done with whatever logic that you have to display the first question. See the code for the Survey action method:

public ViewResult Survey()
{
    // this is where you'll decide to get the first question from your database
    // I cannot comment on this as I'm not sure how your db is structured
}

The issue of Next question comes only when the user has answered the first one. So you do not need to call the GetNextQuestion() for the first question.

>> next or back button
Yes, that code is exactly for that purpose. Depending on what button the user clicks, the appropriate value will be populate in the FormCollection. So if the user clicks on the next button, then formCollection["next"] will have the value 'Next >>' and likewise.
0
 

Author Comment

by:ToString1
ID: 33576139
Excellent answers - you have more than earned your points.
I will be posting more questions about this survey app in the MVC zone later this afternoon

Thank you
0

Featured Post

Scale it in WD Gold

With up to ten times the workload capacity of desktop drives, WD Gold hard drives employ advanced technology to deliver among the best in reliability, capacity, power efficiency and performance.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now