ActionResult没有返回预期的JSON

时间:2013-03-04 05:15:56

标签: c# asp.net-mvc-4

长时间听众,第一次打电话......

最近继承了一个按照MVC方法在VS2010中开发的项目。在“微软方法”中绕过我的脑袋是相当成功的。但是,有一个问题成功阻止了我。

目标:更改填充下拉列表的项目的格式。

在以下javascript中调用操作(位于文件“Participant.cshtml”中):

        var selectedCourse = $('#ddlCourse option:selected').val();
    if (selectedCourse != null && selectedCourse != '' && selectedCourse != '--Select--') {
        $.getJSON('@Url.Action("Unit")', { courseid: selectedCourse }, function (units) {
            var unitcodeSelect = $('#ddlunitcode');
            unitcodeSelect.empty();
            unitcodeSelect.append($('<option/>', {
                value: '--Select--',
                text: '--Select--'
            }));
            $.each(units, function (index, unit) {
                unitcodeSelect.append($('<option/>', {
                    value: unit.Value,
                    text: unit.Text
                }));
            });
        });
    }

现在,据我了解,调用了以下函数(在“ParticipantConroller.cs”文件中):

        public ActionResult Unit(int courseid)
    {
        CoachPortalEntities scope = new CoachPortalEntities();
        var list = scope.tblunits.Where(x => x.courseid == courseid).Select(x => new { ID = x.unitid, value = x.unitcode + " " + x.unitdesc }).Distinct();
        var jlist = list.AsEnumerable().Select(c => new SelectListItem()
        {
            Text = c.value.ToString(),
            Value = c.ID.ToString()
        });
        return Json(jlist, JsonRequestBehavior.AllowGet);

似乎没有其他对“ActionResult Unit”的引用,所以这肯定是正确的代码吗?但是,如果我编辑“var list = ...”行(例如,在 unitcode unitdesc 之间的空格中插入随机字符,或将后者包含在括号中 - 结果的微不足道的格式化,似乎没有出现任何变化......我是否误解了这种情况?

如果有人提出这个问题,请道歉。然而,经过两(2)天的搜索,我找不到类似的东西。如果我错过了什么,那么请随时“带我去学校”! ; - )

0 个答案:

没有答案