What you need to do is separate out what you want to do in C# and do that server side, then send through to the client side the code to be displayed.
For instance:
public class MyModel {
public string JsonArray {get;set;}
}
public class MyController {
public ActionResult MyView(List<items> someList) {
// items will be x in this context, feel free to call it item :)
var jsonString = JsonConvert.Serialize(someList.select(x => new {
someValue = x.someValue
}));
return View(new MyModel { JsonArray = jsonString });
}
}
JsonConvert is from the NewtonSoft library that is packaged in every MVC 4 project. It's so useful for passing JSON friendly data to the client side. You should only use the client side to display information, try and get the server side to process the data ready for client side, hence the MODEL, VIEW, CONTROLLER approach. View should have very little if no knowledge of server side things.
Services is a good way to do this. Dependency Injection, but these are more complicated, read something from Apress books.
In the client side:
@model {namespace}.MyModel
<script>
var arrayToIterate = '@Html.Raw(Model.JsonArray)'
// then do some JQuery or JavaScript iteration here:
<script>
Here is a jsfiddle of when you have got the data to the client side and you want to use knockout to bind it to a table. Really easy for display purposes