1

We have a legacy system developed in ASP.NET, using web forms, that we are in the process of re-designing and would like to re-write the application using ASP.NET MVC.

I am currently going through Steven Sanderson's "Pro ASP.NET MVC2 Framework" getting myself up to speed.

The application is a customer survey form that generates questions and answers from a sqlserver database, based on region, and type of device. The application uses .ascx controls, "asp:PlaceHolder" controls and javascript to hide and un-hide questions based on user input.

(rb2.Attributes.Add("onclick", "show_rb010followup('" + strWebCtrl + "',1);");)

The web site offers dynamic generated questions and follow-up questions based on end user answers. For example. If a user selects "Somewhat dissatisfied" or "Very dissatisfied" in response to a question, a follow up question is un-hidden to prompt the user for further information.

The .ascx controls of course have code behind pages that process the control logic.

My question to all of you, would be to get some ideas on the best way to implement these dynamic question and answers using the MVC design pattern.

Joe Pitz
  • 2,434
  • 3
  • 25
  • 30

2 Answers2

3

I recently developed a system containing this. It's slightly more difficult in MVC than WebForms as MVC doesn't pretend to be stateful (in WebForms you could just programatically create a new control inside the containing control). EDIT - as jfar has pointed out, it's not actually more difficult but does require a different approach and appreciation of web technologies.

Each question can be configured to have a prerequisite / answer. This means that you might have a question with answers to pick from (what colour is your hair? options: red, blonde, brown etc) and then another question with the previous question as a prerequisite and the answers red and blonde and prerequisite answers.

When each question is completed the code goes through and works out what the next question should be based on the sort order and the satisfied prerequisites.

Obviously none of that is MVC-specific.

So far as UI-interaction goes I have some jQuery which sends AJAX requests to the server in order to update its state and to get the details for the next question. I return a partial view from the AJAX request for the jQuery to render. The partial view is in charge of what type of view to render (some might disagree with this and will say that a controller should render different views) in that it checks the enum which defines what sort of input to create and renders the HTML for that input.

I'd suggest making the controller capable of re-rendering the current state of the custom answers if the user refreshes their page (the view the serves the GET request for this page should check the session to see if any question have already been completed).

This obviously wouldn't be fully accessible so you'd need a slightly modified solution to make this work without Javascript enabled but I won't elaborate unless it's of interest.

Let me know if there's any bit of code in particular that you'd be interested in seeing.


As an aside you could look into one of these two projects: MVC Dynamic Forms and MVC XForms. I tried them briefly but decided to write my own. They come recommended in another answer on SO though.

Community
  • 1
  • 1
David Neale
  • 16,498
  • 6
  • 59
  • 85
  • -1er - it's polite to give an explanation of your displeasure. I know the answer doesn't give a concise coded example but that would need a whole blog entry. This is merely meant to be an overview of the solution and I've invited requests for a portion of the code. – David Neale Dec 22 '10 at 09:08
  • David, this too is a pet peave of mine - knocking down the post without expalining why. i've accordingly given you a +1 to redress the balance as part of my 'protest' against this type of thing. i think it should be required to give a one line reason for downvoting, even if it left out the username in order to allow peeps to downvote without fear of reprisal! – jim tollan Dec 22 '10 at 09:21
  • +1 for me as well, trouble is even if you enforce a 1 liner what's to stop someone typing `qwerty`. Some people just downvote regardless. – Rippo Dec 22 '10 at 10:43
  • FYI, this one has been discussed in some depth: http://meta.stackexchange.com/questions/135/encouraging-people-to-explain-down-votes – David Neale Dec 22 '10 at 11:43
  • Fine. Downvoted for perpetuating the myth that state tracking with MVC is hard, difficult or more time consuming than WebForms. The OPs question said it all, time spent learning WebForms control hierarchies and placeholders could have been spend learning effective ajax, session and cookies, the backbone of state management in every other web language and web framework. @David Neale, @Jim, @Rippo – John Farrell Dec 22 '10 at 13:50
  • @jfar. 1. Don't get defensive, this is mean't to be a community to share knowledge and your really comment is appreciated. 2. Maybe I should backup my statement about state management - it might not be harder but certainly requires a difficult approach and greater appreciation of web communication. I make the assumption that the OP knows WebForms well and he has stated that he is learning MVC, therefore different will equal difficult. This is a daft horse to flog though, it's a subjective opinion which has little to do with my actual answer. – David Neale Dec 22 '10 at 14:04
  • In addition, it can often be more complex. The technique is similar to that of a cascading dropdown. In WebForms it is very simple to create one of these without a dependency on Javascript, in MVC you have more control (which is good) but you cannot throw things together so easily as with WebForms events. In fact, I think it's very wrong to jump on the MVC bandwagon and dismiss the fact that WebForms can still be a better fit for many situations. – David Neale Dec 22 '10 at 14:10
  • @David Neale - Cascading dropdowns can't work without javascript. There is no way to submit a form when a dropdown selection changes. Your example is poor. Have another WebForms are a better fit example? – John Farrell Dec 22 '10 at 14:33
  • You would have a select button next to each dropdown to submit the form. That's common practice to make WebForms accessible. – David Neale Dec 22 '10 at 14:49
  • @David Neale - Thats just as easy in MVC. Trivial. – John Farrell Dec 22 '10 at 14:53
  • 'just as easy' = subjective. I guess you think it's daft that ScottGu's team are continuing to use WebForms? – David Neale Dec 22 '10 at 14:59
  • @David Neale - `` is just as easy as `` - Not sure what you mean about ScottGu's team using WebForms, you mean they didn't re-write their blog engine? – John Farrell Dec 22 '10 at 15:07
  • Sorry, I meant continue to develop. The point I was making is that you then have to track the state yourself in MVC whereas you can simply handle the event and bind controls in WebForms. I haven't written a new project in WebForms over MVC for over a year now, but I'm not saying that I wouldn't consider it. – David Neale Dec 22 '10 at 15:12
  • @David Neale - Well WebForms continued support is a business decision. Cut 8 years of legacy apps off from updates? I don't think MS is that crazy. - Everytime you have !IsPostBack your tracking state yourself and handling posts vs gets differently. This is the same in MVC. You have a GET and POST actions which usually share the same view and pass state via formcollections and models. What Webforms does with controls you do via Model Binding and http verb filters. The code in !IsPostBack blocks is similar to what you'd find in any POST gated action method. – John Farrell Dec 22 '10 at 15:28
  • Good point. I guess it's simply the ease of using event handlers that I'm getting at. – David Neale Dec 22 '10 at 15:33
  • @jar, where do you get off making assumptions, about something, without getting all the facts. In your comment "The OPs question said it all, time spent learning WebForms control hierarchies and placeholders could have been spend learning effective ajax, session and cookies, the backbone of state management in every other web language and web framework." I did not write this code, I am inheriting it. I think you just like to belittle people to make yourself feel more important. – Joe Pitz Dec 22 '10 at 18:15
  • + 1 Thanks David for all of the positive feedback and MVC Dynamic form examples – Joe Pitz Dec 22 '10 at 18:29
1

You can use ajax (in jQuery $.ajax) to load a partial view.

Post the answers to given answers using ajax, to an action that interprets the given answers, and returns html that should be visible in reply. Replace or add the response from the server.

IMHO: this will create a more clear understanding of what is happening on the server as using the .Aspx scenario.

GvS
  • 52,015
  • 16
  • 101
  • 139