asp.net mvc 4 - Display with alert the values stored in ViewModel -


my application has many items in viewmodel , see values displayed in alert box can see stored in there. when trying pass view model controller not seeing values, instead getting error 'viewmodel' not defined.

here example of view model script

$(document).ready(function () {     function viewmodel() {          var self = this;          self.sectionid = ko.observable("");         self.sectionname = ko.observable("");          var sectionnames = {             id: self.sectionid,             name: self.sectionname         };          self.selectedsectionname = ko.observable();         self.sectionnames = ko.observablearray();          // initialize view-model work sections         $.ajax({             url: '@url.action("getsections", "home")',             cache: false,             type: 'get',             contenttype: 'application/json; charset=utf-8',             data: {},             success: function (data) {                 self.sectionnames(data);             }         });      var viewmodel = new viewmodel();     ko.applybindings(viewmodel);      }); 

similar creating global variable or class level variable have viewmodel contain of current values of observable items.

except missing closing tag (as mentioned @csharper) code not throw error speak of. answer question lies either in missing closing tag, or in piece of code not shown.

here's how can check, mixing code (+ closing tag) $.ajax stub:

$.ajax = function() {}; // noop stub.    $(document).ready(function () {      function viewmodel() {            var self = this;            self.sectionid = ko.observable("");          self.sectionname = ko.observable("");            var sectionnames = {              id: self.sectionid,              name: self.sectionname          };            self.selectedsectionname = ko.observable();          self.sectionnames = ko.observablearray();            // initialize view-model work sections          $.ajax({              url: '@url.action("getsections", "home")',              cache: false,              type: 'get',              contenttype: 'application/json; charset=utf-8',              data: {},              success: function (data) {                  self.sectionnames(data);              }          });      }              var viewmodel = new viewmodel();      ko.applybindings(viewmodel);       });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>  $root = <code data-bind="text: ko.tojson($root)"></code><br />  no errors on console.


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -