Get ViewData in jQuery View

I have a generic list and I pass it as ViewData from my controller to my .aspx View. I need to get and repeat it using jQuery Script.

How can I make this script work?

Hi

success: function (result) { var myArray = new Array(); var myArray = '<%: ViewData["List"] %>'; for (var i = 0; i < myArray.length; i++) { alert(i); } 
+5
source share
5 answers

Well, if you use only a list of strings, then

it will do

 $(document).ready(function () { @{List<string> listFromController = (List<string>)ViewData["List"];} var myArray = [ @for (int i = 0; i < listFromController.Count; i++) { @: '@(listFromController[i])', } ] }); 

But if you are passing a list of a different type, and not a string, such as an Employee student or user, you will need the following

Please use the appropriate class that you passed, and the properties suggest that "UserName" could be "FirstName", "EmpId" or someday

 $(document).ready(function () { @{ var listFromController = (List<KnockoutJSWebApi.Models.LoginViewModel>)ViewData["list"];} var totalArray = []; @for (int i = 0; i < listFromController.Count; i++) { <text> var thisArray= { 'username': '@(listFromController[i].UserName)', 'password': '@(listFromController[i].Password)' }; totalArray.push(thisArray); </text> } }); 

Syntax for Aspx View Engine:

  <script> $(document).ready(function () { <% List<string> listFromController = (List<string>)ViewData["List"]; %> var myArray = [ <% for (int i = 0; i < listFromController.Count; i++){ %> '<%: listFromController[i] %>', <% } %> ] debugger; }); </script> 
+2
source

Try this, drag and drop list items into javascript array, wrap below code in script tag

 --script tag-- var jList = new Array(); @foreach (var item in ViewData["List"] as List<String>) { @:jList.push('@item'); // That will work if its a one letter string but It doesnt know that its a string untill you use '' } --script tag-- 
+1
source

You can do something like this.

 <script type="text/javascript"> @{ List<string> ss = (List<string>)ViewData["List"];} var myArray = [ @for (int i = 0; i < ss.Count; i++) { @: @(ss[i]), } ] </script> 

my syntax for viewing razors (you can do it for classic)

+1
source

The problem is resolved. I used the JsonResult method in my controller and returned the necessary values ​​as:

 Json(List, JsonRequestBehavior.AllowGet); 

I don’t need to iterate the values, because I assign them as a data source for my DropDownList control.

 $("#Generic_FK").data("DropDownList").dataSource.data(result); 

Hope to help other people with the same problem!

+1
source

Server side

  public class users { public string name{ get; set; } } // in your controller code ViewData["list"] = new List<users>(new users[] { new users() { name="prasad"}, new users() {name="raja"}}); 

Client side

 <script type="text/javascript"> $(function(){ var myitems=JSON.parse('@Html.Raw(Json.Encode(this.ViewData["list"]))'); $.each(myitems, function(index, item) { var name = item.name; }); }); </script> 

Hope this helps you.

0
source

All Articles