c# - Get ViewData in View JQuery -
i have generic list , i'm passing viewdata controller .aspx view. need , iterate jquery script.
how make script works?
regards
success: function (result) { var myarray = new array(); var myarray = '<%: viewdata["list"] %>'; (var = 0; < myarray.length; i++) { alert(i); }
ok if using list of strings
this do
$(document).ready(function () { @{list<string> listfromcontroller = (list<string>)viewdata["list"];} var myarray = [ @for (int = 0; < listfromcontroller.count; i++) { @: '@(listfromcontroller[i])', } ] });
but if passing list of type rather string student employee or user need following
please use appropriate class have passed , properties suppose "username" "firstname" "empid" or ever
$(document).ready(function () { @{ var listfromcontroller = (list<knockoutjswebapi.models.loginviewmodel>)viewdata["list"];} var totalarray = []; @for (int = 0; < listfromcontroller.count; i++) { <text> var thisarray= { 'username': '@(listfromcontroller[i].username)', 'password': '@(listfromcontroller[i].password)' }; totalarray.push(thisarray); </text> } });
aspx view engine syntax:
<script> $(document).ready(function () { <% list<string> listfromcontroller = (list<string>)viewdata["list"]; %> var myarray = [ <% (int = 0; < listfromcontroller.count; i++){ %> '<%: listfromcontroller[i] %>', <% } %> ] debugger; }); </script>
Comments
Post a Comment