c# - How to Dynamically Load a String from a model to javascript in MVC3? -


okay last time asked question on mvc3 got extremely down voted , have no idea why, if goes same way, please give me reason can fix it. anyway, i'm try take string model , put javascript. javascript manipulates string. have javascript working static strings in array, wish have these strings in sql database , load model , there put javascript doing bulk of work.

here javascript:

function questionviewmodel() {     this.questions = new array("whats difference between axial leads , radial leads?", "what difference between ac , dc current?", "is nikola telsa known work ac current or dc current.", "what semiconductor?");     this.answers = new array();     this.answers[0] = new array("radial leads point in same direction, while axial leads point in oppossite directions", "radial leads round, while axial leads square", "there no difference");     this.answers[1] = new array("dc has current flowing in 1 direction, while ac has flow of current changing.", "ac has current flowing in 1 direction, while dc has flow of current changing.", "ac weaker dc");     this.answers[2] = new array("ac", "dc", "both");     this.answers[3] = new array("a material has electrical conductivity between of metal , insulator.", "something reduces flow of current", "a large wire");     this.right = new array("radial leads point in same direction, while axial leads point in oppossite directions", "dc has current flowing in 1 direction, while ac has flow of current changing.", "ac", "a material has electrical conductivity between of metal , insulator.");     this.i = math.floor(math.random() * this.questions.length);     this.a = math.floor(math.random() * this.answers[0].length);     var ians1 = this.i;     var ians2 = this.i;     var ians3 = this.i;     this.currentquestion = this.questions[this.i];     this.nextquestion = "";     this.answer1 = ko.observable(this.answers[this.i][0]);     this.answer2 = ko.observable(this.answers[this.i][1]);     this.answer3 = ko.observable(this.answers[this.i][2]); } 

so i'm hoping load strings questions , answers arrays model, imagine done in controller part, script append view.

if can extremely appreciated, , way, i'm new .net , mvc3 need can get. thank you.

i did last friday, it'll hard describe being it's monday , got work, anyway here's solution ended using:

controller:

public viewresult questionare()     {         goldienator.models.questionnairemodel model = new models.questionnairemodel();         model.question1 = "whats difference between axial leads , radial leads?";         model.question2 = "what difference between ac , dc current?";         model.question3 = "is nikola telsa known work ac current or dc current?";         model.question4 = "what semiconductor?";         model.answer11 = "radial leads point in same direction, while axial leads point in oppossite directions";         model.answer12 = "radial leads round, while axial leads square";         model.answer13 = "there no difference";         model.answer21 = "dc has current flowing in 1 direction, while ac has flow of current changing.";         model.answer22 = "ac has current flowing in 1 direction, while dc has flow of current changing.";         model.answer23 = "ac weaker dc";         model.answer31 = "ac";         model.answer32 = "dc";         model.answer33 = "both";         model.answer41 = "a material has electrical conductivity between of metal , insulator.";         model.answer42 = "something reduces flow of current";         model.answer43 = "a large wire";         return view(model);     } 

model:

public class questionnairemodel {     public string question1;     public string question2;     public string question3;     public string question4;     public string answer11;     public string answer12;     public string answer13;     public string answer21;     public string answer22;     public string answer23;     public string answer31;     public string answer32;     public string answer33;     public string answer41;     public string answer42;     public string answer43; } 

view:

<div style="margin:0 20px 0 20px;" >     <input type="text" id="question1" style="display: none;" value="@model.question1">     <input type="text" id="question2" style="display: none;" value="@model.question2">     <input type="text" id="question3" style="display: none;" value="@model.question3">     <input type="text" id="question4" style="display: none;" value="@model.question4">     <input type="text" id="answer11" style="display: none;" value="@model.answer11">     <input type="text" id="answer12" style="display: none;" value="@model.answer12">     <input type="text" id="answer13" style="display: none;" value="@model.answer13">     <input type="text" id="answer21" style="display: none;" value="@model.answer21">     <input type="text" id="answer22" style="display: none;" value="@model.answer22">     <input type="text" id="answer23" style="display: none;" value="@model.answer23">     <input type="text" id="answer31" style="display: none;" value="@model.answer31">     <input type="text" id="answer32" style="display: none;" value="@model.answer32">     <input type="text" id="answer33" style="display: none;" value="@model.answer33">     <input type="text" id="answer41" style="display: none;" value="@model.answer41">     <input type="text" id="answer42" style="display: none;" value="@model.answer42">     <input type="text" id="answer43" style="display: none;" value="@model.answer43">     click start asked question, if enough right, recieve discount!     <button id="start" value="start" onclick="start()">start</button>     <p data-bind="text: currentquestion"></p>     <label>answer:</label>     <div>         <input type="radio" id="answer1" name="ans" style="margin: 10px;" data-bind="value: answer1()"><span data-bind="text: answer1()"></span><br>         <input type="radio" id="answer2" name="ans" style="margin: 10px;" data-bind="value: answer2()"><span data-bind="text: answer2()"></span><br>         <input type="radio" id="answer3" name="ans" style="margin: 10px;" data-bind="value: answer3()"><span data-bind="text: answer3()"></span><br>     </div>     <button id="answersubmit" value="submit" onclick="questionare()" style="margin: 10px;">submit</button>     <p id="message"></p>     <p id="message2"></p>     <p style="float: right;">question <span id="counter"></span></p> </div> 

javascript:

function questionviewmodel() {     this.questions = new array(document.getelementbyid('question1').value, document.getelementbyid('question2').value, document.getelementbyid('question3').value, document.getelementbyid('question4').value);     this.answers = new array();     this.answers[0] = new array(document.getelementbyid('answer11').value, document.getelementbyid('answer12').value, document.getelementbyid('answer13').value);     this.answers[1] = new array(document.getelementbyid('answer21').value, document.getelementbyid('answer22').value, document.getelementbyid('answer23').value);     this.answers[2] = new array(document.getelementbyid('answer31').value, document.getelementbyid('answer32').value, document.getelementbyid('answer33').value);     this.answers[3] = new array(document.getelementbyid('answer41').value, document.getelementbyid('answer42').value, document.getelementbyid('answer43').value);     this.right = new array(document.getelementbyid('answer11').value, document.getelementbyid('answer21').value, document.getelementbyid('answer31').value, document.getelementbyid('answer41').value);     this.i = math.floor(math.random() * this.questions.length);     this.a = math.floor(math.random() * this.answers[0].length);     var ians1 = this.i;     var ians2 = this.i;     var ians3 = this.i;     this.currentquestion = this.questions[this.i];     this.nextquestion = "";     this.answer1 = ko.observable(this.answers[this.i][0]);     this.answer2 = ko.observable(this.answers[this.i][1]);     this.answer3 = ko.observable(this.answers[this.i][2]);     this.nans1 = "";     this.nans2 = "";     this.nans3 = "";     if (this.i + 1 < this.questions.length && this.a + 2 < this.answers[0].length) {         this.nextquestion = this.questions[this.i + 1];         this.nans1 = ko.observable(this.answers[this.i + 1][this.a]);         this.nans2 = ko.observable(this.answers[this.i + 1][this.a + 1]);         this.nans3 = ko.observable(this.answers[this.i + 1][this.a + 2]);         ians1++;         ians2++;         ians3++;     }     else if (this.i + 1 < this.questions.length && this.a + 2 == this.answers[0].length) {         this.nextquestion = this.questions[this.i + 1];         this.nans1 = ko.observable(this.answers[this.i + 1][this.a]);         this.nans2 = ko.observable(this.answers[this.i + 1][this.a + 1]);         this.nans3 = ko.observable(this.answers[this.i + 1][0]);         ians1++;         ians2++;         ians3++;     }     else if (this.i + 1 < this.questions.length && this.a + 2 > this.answers[0].length) {         this.nextquestion = this.questions[this.i + 1];         this.nans1 = ko.observable(this.answers[this.i + 1][1]);         this.nans2 = ko.observable(this.answers[this.i + 1][this.a]);         this.nans3 = ko.observable(this.answers[this.i + 1][0]);         ians1++;         ians2++;         ians3++;     }     else if (this.i + 1 == this.questions.length && this.a + 2 < this.answers[0].length) {         this.nextquestion = this.questions[0];         this.nans1 = ko.observable(this.answers[0][this.a]);         this.nans2 = ko.observable(this.answers[0][this.a+1]);         this.nans3 = ko.observable(this.answers[0][this.a + 2]);         ians1=0;         ians2=0;         ians3=0;     }     else if (this.i + 1 == this.questions.length && this.a + 2 == this.answers[0].length) {         this.nextquestion = this.questions[0];         this.nans1 = ko.observable(this.answers[0][this.a]);         this.nans2 = ko.observable(this.answers[0][this.a + 1]);         this.nans3 = ko.observable(this.answers[0][0]);         ians1 = 0;         ians2 = 0;         ians3 = 0;     }     else if (this.i + 1 == this.questions.length && this.a + 2 > this.answers[0].length) {         this.nextquestion = this.questions[0];         this.nans1 = ko.observable(this.answers[0][1]);         this.nans2 = ko.observable(this.answers[0][this.a]);         this.nans3 = ko.observable(this.answers[0][0]);         ians1 = 0;         ians2 = 0;         ians3 = 0;     }     else {         this.nextquestion = this.questions[0];         this.nans1 = ko.observable(this.answers[0][0]);         this.nans2 = ko.observable(this.answers[0][1]);         this.nans3 = ko.observable(this.answers[0][2]);     }     this.geti = function () { return this.i; };     this.geta = function () { return this.a; };     this.getright = function () { return this.right[this.i]; };     this.getans1 = function () {         return this.answer1();     };     this.getans2 = function () {         return this.answer2();     };     this.getans3 = function () {         return this.answer3();     };     this.setans1 = function () {         this.answer1 = this.nans1;         if (ians1 + 1 < this.answers.length) {             this.nans1 = ko.observable(this.answers[ians1 + 1][0]);             ians1++;         } else {             this.nans1 = ko.observable(this.answers[0][0]);             ians1 = 0;         }     };     this.setans2 = function () {         this.answer2 = this.nans2;         if (ians2 + 1 < this.answers.length) {             this.nans2 = ko.observable(this.answers[ians2 + 1][1]);             ians2++;         } else {             this.nans2 = ko.observable(this.answers[0][1]);             ians2 = 0;         }     };     this.setans3 = function () {         this.answer3 = this.nans3;         if (ians3 + 1 < this.answers.length) {             this.nans3 = ko.observable(this.answers[ians3 + 1][2]);             ians3++;         } else {             this.nans3 = ko.observable(this.answers[0][2]);             ians3 = 0;         }     };     this.getlist = function () {         return this.questions;     };     this.getcurrent = function () {         return this.currentquestion;     };     this.getnext = function () {         return this.nextquestion;     };     this.getlength = function () {         return this.questions.length;     };     this.setquestion = function (x) {         if (this.i + 1 <= this.questions.length - 1) {             this.nextquestion = this.questions[this.i + 1];             this.i++;         } else {             this.nextquestion = this.questions[0];             this.i = 0;         }         this.currentquestion = this.nextquestion;     };  } 

so still test, it's not loading questions database yet, it's using mvc , makes else easy. had use invisible inputs store strings in view have javascript values.


Comments