0

我正在尝试从 jQuery 调用控制器中的服务器端操作:

$.ajax({
            url:'http://localhost:88/admin/business/11/GetChildBusinessTypes',
            data: { parentId: $('#business_parentbusinesstype_id').val() },
            dataType: 'json',
            success: fillChildBusinessTypes,
            error: ajaxError
        });

这是控制器动作:

public string GetChildBusinessTypes(int parentId)
        {
            //get child business types.
            var businessTypes = BusinessTypeRepository.GetChildBusinessTypes(parentId);
            //convert to JSON.
            var serializer = new JavaScriptSerializer();
            return serializer.Serialize(businessTypes);
        }

它给了我这个错误:

MonoRail 无法解析模板“admin\business\GetChildBusinessTypes”的视图引擎实例有两个可能的原因:模板不存在,或者处理特定文件扩展名的视图引擎未正确配置 web.config (部分单轨,节点视图引擎)。

很明显,它正在尝试将操作视为视图并出错。我尝试将其作为 POST 而不是 GET 发送,但收到相同的错误。我需要做什么才能使其正常工作?

谢谢!贾斯汀

4

1 回答 1

1

这是其他希望从 jQuery 调用控制器操作并返回 JSON 的人的答案...

控制器方法:

[return: JSONReturnBinder(Properties = "Id,Name")]
        public BusinessType[] GetChildBusinessTypes(int parentId)
        {
            var businessTypes = BusinessTypeRepository.GetChildBusinessTypes(parentId);
            return businessTypes;
        }

Javascript:

$(document).ready(function () {
        $('#business_parentbusinesstype_id').change(function () {
            jQuery.ajax({
                url: "$UrlHelper.For("%{action='$business.site.id/GetChildBusinessTypes'}")",
                data: { parentId: $('#business_parentbusinesstype_id').val() },
                dataType: 'json',
                type: 'GET',
                success: fillChildBusinessTypes,
                error: ajaxError
            });
        });
    });

    function fillChildBusinessTypes(json) {
        //get business types.
        var businessTypes = eval(json);
        //bind business types to dropdown.
        $("#business_businesstype_id").get(0).options.length = 0;
        $("#business_businesstype_id").get(0).options[0] = new Option("Select a Business Type", "0");
        jQuery.each(businessTypes, function(index, item) {
            $('#business_businesstype_id').get(0).options[$("#business_businesstype_id").get(0).options.length] = new Option(item.Name, item.Id);
        });
        //show child dropdown.
        Show($('#spnChildBusinessTypes'));
    }
于 2010-05-28T14:36:38.213 回答