Solved

Create wizard type navigation in MVC

Posted on 2010-09-01
8
948 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 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

19 Experts available now in Live!

Get 1:1 Help Now