ASP.NET MVC dynamic views
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开发者_如何学JAVA 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.
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.
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.
精彩评论