将List <int>从Model传递给jQuery int [] </int>

时间:2013-07-24 18:12:09

标签: jquery asp.net-mvc-4

我想将模型中的整数列表传递给jQuery。但是,我不确定jQuery语法。任何关于如何填充window.checkedCoursesArray值的帮助将不胜感激。

型号:

public List<int> PrintCourses { get; set; }

控制器:

var listCourseIDs = (from c in listFilteredTREs
                             select c.CourseID).ToList();
homeViewModel.PrintCourses = listCourseIDs;

JQuery的:

window.checkedCoursesArray = new Array();

感谢所有帮助。这是完成的jQuery / Razor代码:

@foreach(var item in Model.PrintCourses)
{
    @:window.checkedCoursesArray.push(@item);
}

1 个答案:

答案 0 :(得分:1)

//控制器:

[HttpGet]
public JsonResult PrintCourses()
{
    var listCourseIDs = new List<int>() { 1,2,3,4,5};

    return Json(listCourseIDs, JsonRequestBehavior.AllowGet);
}

//查看:

<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>

=============================================== =====================

//控制器

public ActionResult Index()
{
    var model = new PrintCourses();
    model.ids = new List<int>() {1, 2, 3, 4, 5};

    return View(model);
}

//视图

@model MvcApplication6.Models.PrintCourses
@foreach (var id in Model.ids)
{
    @id<br/>
}