asp.net mvc 5 - EditorTemplates Where or Groupby on Questionnaire -


at moment, have working prototype questionnaire multiple questions , each question having multiple choices answer. displays , saves great. however, group question/answers 'sections' on edit view. have tried couple different methods nothing seems work correctly. working code without sections follows:

edit view progress report:

<div class="form-group">     @html.labelfor(model => model.reportanswers, htmlattributes: new { @class = "control-label col-md-2" })     <div class="col-md-10">         @html.editorfor(model => model.reportanswers)     </div> </div> 

reportanswers.cshtml (editor template)

<h3>     question </h3>  <p>     @html.displayfor(x => x.question.questiontext) </p> @html.hiddenfor(model => model.questionid) @html.hiddenfor(model => model.reportid)  @foreach (var answer in model.question.possibleanswers) {     var id = string.format("answer-{0}", answer.answerid);     <p>         @html.hiddenfor(model => model.reportanswerid)          @html.radiobuttonfor(m => m.answerid, answer.answerid, new { id = id })         <label for="@id">@answer.answertext</label>     </p> } 

editcontroller:

[httppost]     [validateantiforgerytoken]     public actionresult edit(progressreport progressreport)     {         if (modelstate.isvalid)         {             db.entry(progressreport).state = entitystate.modified;             db.savechanges();              foreach (reportanswer answer in progressreport.reportanswers)             {                         if (answer.questionid != null && answer.answerid != null)                         {                             db.entry(answer).state = entitystate.modified;                             db.savechanges();                         }                                 }              return redirecttoaction("index");         }         viewbag.clientid = new selectlist(db.clients, "clientid", "id", progressreport.clientid);         return view(progressreport);     } 

data structure sections -> questions -> answers have progressreport table has questionid , answerid

i have attempted groupby within view, unsure how call editortemplate correctly. in fact, able use it, result not expected. code is:

@foreach (var group in model.reportanswers.groupby(s => s.question.sectionid)) 

thanks!

data structure snippet: enter image description here

you better of creating view models represents data want display in view

public class reportvm {   public int id { get; set; }   // other properties or report display/editing   public list<sectionvm> sections { get; set; } } public class sectionvm {   public int id { get; set; }   public string name { get; set; }   // other properties or report display/editing   public list<questionvm> questions{ get; set; } } public class questionvm {   public int id { get; set; }   public string title { get; set; }   public int selectedanswer { get; set; }   public list<answer> possibleanswers { get; set; } } 

the in controller, use .groupby() query build view model , add each section , related questions/answers

then add editortempate sectionvm

@model sectionvm @html.hiddenfor(m = > m.id) <h2>@html.displayfor(m => m.name)</h2> @html.editorfor(m => m.questions) // uses editortemplate questionvm (i.e. per current reportanswers template 

and in main view

@html.editorfor(model => model.sections) 

Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -