从控制器传递的数据在视图中显示列表

时间:2019-02-23 03:57:02

标签: c# jquery ajax asp.net-mvc

我正在从控制器传递JSON格式的列表,出于某种原因,列表中的数据未显示在视图中。

这是正在控制器中传递的数据:

 [HttpPost]
        public ActionResult GetIndustryCat(string Country)
        { var dataContext = MvcApplication.GetDataContext();
            var Location = dataContext.Locations.Single(c => c.Location_Name == Country);
            var IndustryCat = dataContext.IndustryCategories.Where(c => c.Location_ID == Location.Location_ID).ToList();
            return Json(new {Cat = IndustryCat.Select(c => c.IndustryCategory_Name) });
        }

这是视图:

</select>
<script>
    $("#selectindustrycat").hide();
    $("select")
        .change(function () {
            var str = "";
            $("select option:selected").each(function () {
                str += $(this).text() + " ";
            });
            $.ajax({
                url: "GetIndustryCat",
                type: "POST",
                data: { Country: str },
                success: function (data) {

                }
            }).done(function (data) {
                for (var i = 0; i < data.length; i++) {
                    $("#selectindustrycat").append('<option value=' + i + '>' + i + '</option>');
                }
                $("#selectindustrycat").show();
            });
    });


</script>

显示选择选项列表,但其中没有数据。

1 个答案:

答案 0 :(得分:1)

您需要在数据之间进行更改。猫

for (var i = 0; i < data.Cat.length; i++) {
                    $("#selectindustrycat").append('<option value=' + i + '>' + i + '</option>');
                }
相关问题