Click here to Skip to main content
14,971,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have list in MVC view and i need to pass this to javascript function, can someone help me out !!!

Updated 9-Jan-17 21:03pm

in Java Script Write this

function GetList()
var siteRoute= $("#hdsiteroute").val() ;// in cshtml or aspx have a hidden field with this id and value as Url.Content("~") this will help in live mode
var url=siteRoute+"YourController/GetList"

 $.ajax({ url: url, data: {yourargument:1}, type: "GET", dataType: "json", async: false, cache: false, error: ajaxError, success: function (result) {
  for (var i = 0; i < result.length; i++) {
        alert(result[0]); // access each item like this. if list is of any complex type the use result[0].Name // say name is a proprty of the complex type

 } });

function ajaxError(jqXHR, textStatus, errorThrown) {

and in controller have

public ActionResult GetList(int yourargument)
           List<int> MyList = new List<int>(); //LIST CAN BE OF ANY COMPLEX DATATYPE
           return Json(MyList, JsonRequestBehavior.AllowGet);
One liner:

var data = [@Html.Raw(String.Join(",", Model.MyArray.Select(i => "'" + i + "'")))];
ChessGirl 10-Jul-15 5:30am
This worked for me! As a newbie, I had to figure out that it went between script tags and I could use it to pick up an array that I'd passed through the ViewBag:

@{int[] graphNumbers = ViewBag.GraphNumbers;}
<script type="text/javascript">
var graphNumbers = [@Html.Raw(String.Join(",", graphNumbers.Select(i => "'" + i + "'")))];
This passed the array through perfectly, and my Chart.js graph now works!

Thanks very much!
You can pass any data from the controller to the view via the viewbag. Simply add a filed to the viewbag and pass the json encoded list to it. In the view use a script tag on the top of it to assign it's value to a javascript variable. There you have it :)
Thanks for the reply, to adding to the question, i am not supposed to use Viewbag or Tempdata and moreover it is not array list it is "ToList<componentobservationtype>"
how do i get this listcollection into javascript function
Zoltán Zörgő 3-Oct-12 15:19pm
This is not a solution, it is a comment to my answer. Please use the proper tools.
1) why can't you use viewbag or tempdata?
2) there is no such thing in JavaScript as list. There are only arrays and objects - thus your list will be array or object in javascript, but that's indifferent in your case
3) you either render your page with the data in it, as I suggested before, or you use ajax to get it from the server on runtime. In the later case you will not use viewbag or tempdata, but hat is an other scenario. Is it this what's interesting you? In that case mr.priyank's answer is your path to take.
var listData = JSON.parse('@Html.Raw(HttpUtility.HtmlDecode(ViewBag.ListData))');

ViewBag.ListData= JsonConvert.SerializeObject(new List<string>() { "ABC", "EFG" });

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900