0

I have this view:

<script type="text/javascript" src='<%: Url.Content("~/Scripts/new.js") %>'></script>
<div class="col1">
  Number of Questions:
</div>
<div class="col2-input" id="no-of-questions-value">
  <input type="hidden" name="NumberOfQuestionsHidden" id="NumberOfQuestionsHidden" value="<%: Model.NumberOfQuestions %>" />
  <%: Html.TextBoxFor(model => model.NumberOfQuestions) %>
</div>
<div class="col3">
  <div id="save-no-of-questions-button">
    <a href="javascript:saveNoOfQuestions();" class="button">Save</a>
  </div>
</div>

and the new.js where I determine the values: this js runs a scorm module and logs each event as I go through the questions in the scorm module, I count all correct and all incorrect then I know how many questions were asked.

 function LogEvent(action,data)
 {
    if (data == "correct") {
       correct_count++;
       alert("incorrect count = " + incorrect_count + " correct count = " + correct_count);
 }

 if (data == "incorrect") {
    incorrect_count++;
    alert("incorrect count = " + incorrect_count + " correct count = " + correct_count);
 }

}

to get the total number of questions i add correct_count and incorrect_count and want to pass this to the view into Html.TextBoxFor(model => model.NumberOfQuestions). I dont know how to go about this. any help please?

thanks

charlie_cat
  • 1,830
  • 7
  • 44
  • 73

1 Answers1

0

I wonder why you present the number of questions in a textbox, but still.. You can update your textbox value by:

document.getElementById('NumberOfQuestions').value = 'whatever you want';

This will work, since the default element ID is name of the model property.

To get the client ID clean way, you can read more here.

Community
  • 1
  • 1
Michal Klouda
  • 14,263
  • 7
  • 53
  • 77
  • I dont understand, the value for NumberOfQuestions gets calculated in the JS? I must put that calculated value in document.getElementById('NumberOfQuestions').value... – charlie_cat Oct 02 '12 at 08:18
  • That's what you ask for above.. You show your javascript code and say that you want to pass some results from javascript to the view.. – Michal Klouda Oct 02 '12 at 08:25
  • so I will have: <%: Html.TextBoxFor(model => model.NumberOfQuestions, new { id = "NoOfQuestions" })%> in the view and in the JS: var noOfQuestions = correct_count + incorrect_count; $('#NoOfQuestions').val() = noOfQuestions; ? thanks – charlie_cat Oct 02 '12 at 08:26
  • Could be.. Just update the last part to $('#NoOfQuestions').val(noOfQuestions); – Michal Klouda Oct 02 '12 at 08:32
  • oo ok i have that line as just: $('#NoOfQuestions').val(noOfQuestions);..fantastic! thanks! I am learning as I go as I am still a noob in all this :) – charlie_cat Oct 02 '12 at 08:41