0

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:

  1. 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 )
  2. by sending the complete model and then put some foreach 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)
            {

            }
        }
    }
Community
  • 1
  • 1
NoWar
  • 36,338
  • 80
  • 323
  • 498
  • 2
    CODE BEHIND? in asp.net-mvc? what is it? – gdoron May 12 '12 at 21:09
  • @gdoron I mean to create all controls within public ActionResult Index() method and just pass it into the model. – NoWar May 12 '12 at 21:11
  • @gdoron You can read it here ie http://stackoverflow.com/questions/2512887/is-it-a-good-practice-to-write-html-using-a-stringbuilder-in-my-asp-net-codebehi or even better here http://stackoverflow.com/questions/886728/generating-html-email-body-in-c-sharp – NoWar May 12 '12 at 21:18
  • 2
    I know what `code behind`, but there is no such thing in asp.net- **MVC** – gdoron May 12 '12 at 21:20
  • @gdoron May be I have used an incorrect definition but I guess the problem is simple to understand :) – NoWar May 12 '12 at 21:21
  • I too am confused by your question. Put the data Questions/Answer in the model that you pass to the view. Have view loop on put in whatever controls you need with the Html.HelperMethods() – kenny May 12 '12 at 21:46
  • 1
    @kenny Hmmm Could you detail your answer and put it like an answer please? I need the sample for "Have view loop on put in whatever controls you need with the Html.HelperMethods()" – NoWar May 12 '12 at 21:48
  • @kenny Do u mean the approach represented here http://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx ? – NoWar May 12 '12 at 22:06
  • 1
    Yes, something like that is the 'standard' approach with MVC. – kenny May 12 '12 at 23:11
  • @kenny Put it like an answer I didn't know about it. I guess it will help me. – NoWar May 13 '12 at 01:40

1 Answers1

1

Have you thought about using partial views? Your model could contain the name of the partial view (your template), and be rendered like this:

@Html.Partial(Model.Template)
FSuch
  • 46
  • 1
  • 2