3. Displaying Questions
The application stores the definition of a survey and its questions in table storage. To render the questions in a page in the browser, the application uses the MVC EditorExtensions class.
Tailspin chose
this mechanism to render the questions because it makes it easier to
include additional question types at a later date. |
When the Display action method in the SurveysController class in the TailSpin.Web.Survey.Public project builds the view to display
the survey, it retrieves the survey definition from storage, populates a
model, and passes the model to the view. The following code example
shows this action method.
[HttpGet]
public ActionResult Display(string tenant, string surveySlug)
{
var surveyAnswer = CallGetSurveyAndCreateSurveyAnswer(
this.surveyStore, tenant, surveySlug);
var model =
new TenantPageViewData<SurveyAnswer>(surveyAnswer);
model.Title = surveyAnswer.Title;
return this.View(model);
}
The view uses the EditorExtensions class to render the questions. The following code example shows how the Display.aspx page uses the Html.EditorFor element that is defined in the System.Web.Mvc.EditorExtensions class.
<% for (int i = 0;
i < this.Model.ContentModel.QuestionAnswers.Count; i++ ) { %>
...
<%: Html.EditorFor(m=>m.ContentModel.QuestionAnswers[i],
QuestionTemplateFactory.Create(
Model.ContentModel.QuestionAnswers[i].QuestionType)) %>
...
<% } %>
This element iterates over all the questions that the controller retrieved from storage and uses the QuestionTemplateFactory
utility class to determine which user control (.ascx files) to use to
render the question. The user controls FiveStar.ascx,
MultipleChoice.ascx, and SimpleText.ascx are in the EditorTemplates
folder in the project.
4. Displaying the Summary Statistics
The asynchronous task that generates the
summary statistics from surveys stores the summaries in BLOB storage,
using a separate BLOB for each survey. The Surveys application displays
these summary statistics in the same way that it displays questions. The
following code example shows the AnalyzeSurveysController class in the TailSpin.Web project that reads the results from BLOB storage and populates a model. action method in the
public ActionResult Analyze(string tenant, string surveySlug)
{
var surveyAnswersSummary =
this.surveyAnswersSummaryStore
.GetSurveyAnswersSummary(tenant, surveySlug);
var model =
this.CreateTenantPageViewData(surveyAnswersSummary);
model.Title = surveySlug;
return this.View(model);
}
The view again uses the Html.EditorFor element to render the questions. The following code example shows a part of the Analyze.aspx file.
<% for (int i = 0;
i < this.Model.ContentModel.QuestionAnswersSummaries.Count;
i++ ) { %>
<li>
<%: Html.DisplayFor(m => m.ContentModel
.QuestionAnswersSummaries[i],
"Summary-" + TailSpin.Web.Survey.Public.Utility
.QuestionTemplateFactory.Create
(Model.ContentModel.QuestionAnswersSummaries[i]
.QuestionType))%>
</li>
<% } %>
The user control templates for rendering the summary
statistics are named Summary-FiveStar.ascx, which displays an average
for numeric range questions; Summary-MultipleChoice.ascx, which displays
a histogram; and Summary-SimpleText.ascx, which displays a word cloud.
You can find these templates in the DisplayTemplates folder in the
TailSpin.Web project. To support additional question types, you must add
additional user control templates to this folder.