モノレール - jQueryからサーバー側のメソッドを呼び出す方法

StackOverflow https://stackoverflow.com/questions/2925022

  •  05-10-2019
  •  | 
  •  

質問

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);
        }

これは私にこのエラーを与えています:

モノレールは、テンプレート「admin business getChildBusinessTypes」のビューエンジンインスタンスを解決できませんでした。テンプレートが存在しないか、特定のファイル拡張機能を処理するビューエンジンが正しく構成されていない(セクションモノレール、ノードビューエンギン)。

それがあたかもそれがビューであり、エラーを誤っているかのようにアクションを取得しようとしていることは明らかです。私はそれをGETの代わりに投稿として送信しようとしましたが、同じRRORを受け取りました。これを機能させるために何をする必要がありますか?

ありがとう!ジャスティン

役に立ちましたか?

解決

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'));
    }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top