0

別のドロップダウンにデータを入力するために、ドロップダウンの変更イベントを使用してコントローラーで ActionResult を呼び出そうとしています。

これが私が試したjQueryです:

 $(function () {
        $('#CertificationId').change(function () {
            var data = {
                certificationId: $('#CertificationId').val()

            };

            var certificationId = $('#CertificationId').val();
//            $.post('@Url.Action("AjaxGetCourseOptions", "WorkerCertifications")', {certificationId : certificationId}, $(this).parents('form:first').serialize(), function (data) {
//                //$('#form').children().remove().append(data);
//            }, 'html');

//            var url = '@Url.Action("AjaxGetCourseOptions", "WorkerCertifications")';
//            var certificationId = $('#CertificationId').val();
//            $.post(url, { certificationId: certificationId }, function (result) {
//                alert('success');
//            });

//            $.getJSON('/WorkerCertifications/AjaxGetCourseOptions/SysAdmin/Worker/Certifications/14843/', data, function (result) {
//                alert(result.message);
//            });

            $.getJSON('@Url.Action("AjaxGetCourseOptions", "WorkerCertifications")', data, function (result) {
                alert(result.message);
            });

//            $.getJSON(this.href, data, function (result) {
//                alert(result.message);
//            });
            return false;
        });
    });

コントローラーからのコードは次のとおりです。

public ActionResult AjaxGetCourseOptions( string certificationId )
    {
        var viewData = new WorkerCertificationViewModel
        {
            //CourseOptions = ScheduledCourse.GetActive().ToSelectList(x => x.Id, x => x.Id),
            CourseOptions = ScheduledCourse.GetAvailableCourses(Convert.ToInt64(certificationId)).ToSelectList(x => x.Id, x => x.Id)

        };

        viewModel.CourseOptions = viewData.CourseOptions;

        return Json( new {message = "Test"},
                     JsonRequestBehavior.AllowGet
            );
    }

jQuery でコントローラーのコードを呼び出すことができないようです。どうすればこれを達成できますか?

アップデート

これを機能させるにはまだ問題があります。これは、ドロップダウンの変更イベントが発生する前のページの URL ですhttp://mylocalhost.com/camms/WorkerCertifications/Insert/SysAdmin/Worker/Certifications/14843

変更イベントが発生した後、投稿したい URL を (今のところ) ハードコーディングしましたが、現在の URL に追加されています。これを修正する方法はありますか?投稿しようとしている URL は次のとおりです

URL は次のようになります: http://mylocalhost.com/camms/WorkerCertifications/AjaxGetCourseOptions/SysAdmin/Worker/Certifications/14843/?certificationId=10916

この更新を保存するには、ローカル ホストとポートを削除する必要がありました。

4

2 に答える 2

0

私はこれを次のように達成しました:

Jquery:

$(function () {
    $('#CertificationId').change(function (evt) {
        var data = {
            certificationId: $('#CertificationId').val()

        };

        var certificationId = $('#CertificationId').val();
        $.ajax({
            //url: "/camms/WorkerCertifications/GetCourseOptions/SysAdmin/Worker/Certifications/14843",
            url: window.location.href.replace('Insert', 'GetCourseOptions'),
            type: 'POST',
            data: { certificationId: certificationId },
            success: function (courseOptions) {
                // courseOptions is your JSON array
                var $select = $('#CourseId');
                $select.children().remove();
                if (courseOptions.length > 0) {
                    var listItems = [];
                    for (var i = 0; i < courseOptions.length; i++) {
                        listItems.push('<option value="' +
                            courseOptions[i].Value + '">' + courseOptions[i].Text + '</option>');
                    }
                    $select.append(listItems.join(''));
                }

                //                    $.each(courseOptions, function (i, option) {
                //                        $('<option>', {
                //                            value: option.Id
                //                        }).html(option.Id).appendTo($select);
                //                    });
            }
        });

        return false;
    });
});

コントローラの場合:

[HttpPost]
    public JsonResult GetCourseOptions( string certificationId = "0")
    {
        var list = ScheduledCourse.GetAvailableCourses(Convert.ToInt64(certificationId)).ToSelectList(x => x.ScheduledCourseId, x => x.ScheduledCourseId);

        var result = new JsonResult();

        result.Data = list.ToList();

        return result;
    }
于 2013-03-22T18:46:36.593 に答える