I do some webpage under MVC3 and I got some stuck about the best way to generate HTML controls for the questionnaire.
I guess there are 2 ways:
- by code to generate raw html with controls and etc. (I mean like
sb.Append("<input id='bla' class='bla' />") and etc..
Sample is here ) - by sending the complete
model
and then put someforeach
inside of HTML code
Could u suggest please which way is better and how do I have to get the answers then (by model
or FormCollection
)?
Here is my draft for the loop (no other things yet...)
var qs = db.SN_Questionnaires.Where(q => q.Code == 1).FirstOrDefault();
foreach (var questionGroup in qs.SN_QuestionGroups)
{
foreach (var question in questionGroup.SN_Questions)
{
var questionType = db.SN_QuestionTypes.Where(qt => qt.ID == question.SN_QuestionTypeID).FirstOrDefault(); // Get type of the control here (TextBox, Radio, and etc)
foreach (var answer in question.SN_Answers)
{
}
}
}