c# - Converting a part of hard coded sample project in MVC to soft code (pulling the data from the database) -


i'm following along sample project this website. reached point constructing action method in action method hardcoded data using.

i tried think of pull same data database failed. still beginner mvc i'm asking assistance in doing because i've got project in mind , long same line sample project.

the action method looks

public actionresult index() {     var evalvm = new evaluation();      var q1 = new question { id = 1, questiontext = "what favouritelanguage" };     q1.answers.add(new answer { id = 12, answertext = "php" });     q1.answers.add(new answer { id = 13, answertext = "asp.net" });     q1.answers.add(new answer { id = 14, answertext = "java" });     evalvm.questions.add(q1);      var q2 = new question { id = 2, questiontext = "what favourite db" };     q2.answers.add(new answer { id = 16, answertext = "sql server" });     q2.answers.add(new answer { id = 17, answertext = "mysql" });     q2.answers.add(new answer { id = 18, answertext = "oracle" });     evalvm.questions.add(q2);      return view(evalvm);     }        

you need build question , answer repository. 2 tables, question table , answer table question_answer_id links answer question (one many relationship). code pull information database, iterate through each question, create question, iterate through each answer, create answer, add question questions. want results stored, question results table might necessary links user answers questions later reporting.

question

 question_id  description 

answer

answer_id question_answer_id description 

create objects/entities, represent columns, , repository getting information.

public class question {     public int questionid { get; set; }     public string description { get; set; } }  public class answer {     public int answerid { get; set; }     public int questionanswerid { get; set; }     public string description { get; set; } }  public class questionrepository : yourdbconnection {     public list<question> getquestions()     {         list<question> question = new list<question>();         string sql = "select * question";         connection connection = getyourdbconnection();         command command = connection.createcommand();         command.commandtext = sql;         datareader reader = null;         try         {             connection.open();             reader = command.executereader();             while(reader.read())             {                  question q = new question();                  q.questionid = convert.toint32(reader.getvalue(reader.getordinal("question_id")));                  q.description = reader.getvalue(reader.getordinal("description")).tostring();                  questions.add(q);             }             reader.close();             connection.close();         }         catch(exception ex) //readers don't throw database exceptions when attempting info index         {             if(reader != null)                 if(!reader.isclosed)                     reader.close();             connection.close();             logger.error("failed retrieve questions database.", ex);         }         return questions;     } } 

pretty same public list<answer> getanswers()...

then use linq when building questionnaire.

questionrepository questionrepo = new questionrepository(); answerrepository answerrepo = new answerrepository(); list<question> questions = questionrepo.getquestions(); list<answer> answers = answerrepo.getanswers();  foreach(question q in questions) {    var q1 = new question { id = q.questionid, questiontext = q.description };    list<answer> questionanswers = answers.findall(o => o.questionanswerid == q.questionid);     if(questionanswers != null)     {         foreach(answer in questionanswers)         {             q1.answers.add(new answer { id = a.answerid, answertext = a.description });             evalvm.questions.add(q1);          }     }     return evalvm; } 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -