title: ViewBag on a Javascript code mvc 4
body: <p>I am having a trouble doing this: I have a ViewBag that contains the list of documents Id's and the response time for each one, created like this:</p>
  
  <p><strong>Controller</strong>:</p>
  
  <pre><code>ViewBag.Documents= (from n in db.DocumentType
                       select new {a = n.DocumentTypeId, b=n.ResponseTime});
  </code></pre>
  
  <p>Based on the document that the user selected on a dropdownlist, the javascript code calculates the real duration of processing the document (duration).</p>
  
  <pre><code>DocumentId(int)          Response(int)
      1                     2 days
      2                     3 days
      3                     1 day
  </code></pre>
  
  <p>The complete javascript code calculates the final date based on response time without weekends and holidays, but the only part that it is not working is the following part of JavaScript code:</p>
  
  <p><strong>JavaScript at the view:</strong></p>
  
  <pre><code>function CalculateTime(documenttype) {
                  var duration= 0;
                  var jScriptArray= new Array();
                  var array = '@Html.Raw(Json.Encode(ViewBag.Documents))';
                  for (var i = 0; i &lt; array.length; i++) {
                      for (var j = 0; j &lt; 2; j++) {
                          jScriptArray [i][j] = array [i][j];
                      }
                  }
                  for (i = 0; i &lt; jScriptArray.length; i++) {
                     if (documenttype== jScriptArray [i][0]) duration= jScriptArray [i][1];
                  }
                  return duration;
              }
  </code></pre>
  
  <p>I already have the correct <code>documenttype</code> variable based on a dropdownlist but this script is not working. I think is the way I want to use the ViewBag, but I am not pretty sure. What I need to know is: what is wrong with my code or another way to do this.</p>
  
  <p>Thanks in advance</p>