Reputation: 3071
I would like to pass a list of integers from my Model to jQuery. But, I am unsure of the jQuery syntax. Any help on how to fill the window.checkedCoursesArray values would be appreciated.
Model:
public List<int> PrintCourses { get; set; }
Controller:
var listCourseIDs = (from c in listFilteredTREs
select c.CourseID).ToList();
homeViewModel.PrintCourses = listCourseIDs;
JQuery:
window.checkedCoursesArray = new Array();
Thanks for all the help. Here is the finished jQuery/Razor code:
@foreach(var item in Model.PrintCourses)
{
@:window.checkedCoursesArray.push(@item);
}
Upvotes: 3
Views: 4205
Reputation: 18387
//controller:
[HttpGet]
public JsonResult PrintCourses()
{
var listCourseIDs = new List<int>() { 1,2,3,4,5};
return Json(listCourseIDs, JsonRequestBehavior.AllowGet);
}
//view:
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var url = '@Url.Action("PrintCourses")';
$.get(url, function(data) {
for (i = 0; i < data.length; i++) {
alert(i);
}
});
});
</script>
====================================================================
//Controller
public ActionResult Index()
{
var model = new PrintCourses();
model.ids = new List<int>() {1, 2, 3, 4, 5};
return View(model);
}
//View
@model MvcApplication6.Models.PrintCourses
@foreach (var id in Model.ids)
{
@id<br/>
}
Upvotes: 1