I am trying to create an online survey.
I have a View where a user can create a Multiple Choice question with as many answer possibilities as the user wishes. Each answer possibility is supposed to have text boxes which represents the following properties:
[Multiple_Choice_Question]
int choice_number { get; set; } // The order in which the MCQ answer possibility is shown
int choice_wording { get; set; } // The MCQ answer possibility
string help_text { get; set; } // help_text if the user doesnt understand the answer possibility
How should my ViewModel look like, when i dont know how many answer possibilities the user wants?
Thanks
Use javascript and the JSON model binding in MVC3 to help you here.
On the client side create a javascript “object” that matches your server side object
Then use javascript to iterate and parse through the DOM to get the variable number of answers.
psuedo-code…
Post this with ajax. One downside to this method is that you have to use ajax.
Then on the server side you have your question class as you’ve already defined it(your property names must match the client side property names), and a container class…
and your Action takes this as a parameter
Phil Haack did an article about this, but before the MVC3 binding was released. It’s easier now than what he wrote about.
http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx