调用控制器,根据DropDownList填充文本框,以便使用Ajax进行选择

调用控制器,根据DropDownList填充文本框,以便使用Ajax进行选择,ajax,asp.net-mvc,model-view-controller,jquery,Ajax,Asp.net Mvc,Model View Controller,Jquery,我有一个下拉列表,当我从中选择一个项目时,我想将所选的值传递给控制器中的一个函数,查询数据库并自动加载带有查询结果的文本框 当下拉列表中出现onclick事件时,如何使用Ajax调用控制器 我的aspx页面中的下拉列表和文本框是: 我在控制器中的功能是 您可以为下拉列表提供id和url: <%= Html.DropDownListFor( model => model.ApplicationSegmentGuid, Model.ApplicationSegment,

我有一个下拉列表,当我从中选择一个项目时,我想将所选的值传递给控制器中的一个函数,查询数据库并自动加载带有查询结果的文本框

当下拉列表中出现onclick事件时,如何使用Ajax调用控制器

我的aspx页面中的下拉列表和文本框是:

我在控制器中的功能是


您可以为下拉列表提供id和url:

<%= Html.DropDownListFor(
    model => model.ApplicationSegmentGuid, 
    Model.ApplicationSegment,
    new { id = "myddl", data_url = Url.Action("AsyncFocalPoint") }
) %>

事实上,我有两个下拉列表具有级联效果,上面提到的ddl是基于之前的下拉列表选择加载的。在我尝试了您提供的代码后,它停止加载下拉列表。下面是我为dropdownload$ApplicationSegmentGuid.CascadingDropDownWorkActionGuid的ajax异步调用提供的代码@user1251759,您注意到我将ddl的id更改为myddl了吗?因此,调整您的选择器:$'myddl'.CascadingDropDown。。。。
 public ActionResult AsyncFocalPoint(Nullable<Guid> ApplicationSegmentGuid)
    {
        string tempEmail = UnityHelper.Resolve<IUserDirectory>().EmailOf();
        tempEmail = "subbulakshmi.kailasam@lyondellbasell.com" + tempEmail;

        IList<string> EmailAddresses = new List<String>();

        using (TSADRequestEntities context = UnityHelper.Resolve<TSADRequestEntities>())
        {
            EmailAddresses = context.FOCALPOINTs.Where(T => T.APPLICATIONSEGMENT.ItemGuid == ApplicationSegmentGuid && T.FlagActive)
                      .Select(T => T.Email).ToList();
        }
        foreach (string emailAddress in EmailAddresses)
            tempEmail = tempEmail + ";" + emailAddress;

        return Json(tempEmail, JsonRequestBehavior.AllowGet);
    }
<%= Html.DropDownListFor(
    model => model.ApplicationSegmentGuid, 
    Model.ApplicationSegment,
    new { id = "myddl", data_url = Url.Action("AsyncFocalPoint") }
) %>
$(function() {
    $('#myddl').change(function() {
        // get the selected value of the ddl
        var value = $(this).val();

        // get the url that the data-url attribute of the ddl
        // is pointing to and which will be used to send the AJAX request to
        var url = $(this).data('url');

        $.ajax({
            url: url,
            type: 'POST', 
            data: { applicationSegmentGuid: value },
            success: function(result) {
                // TODO: do something with the result returned by the server here
                // for example if you wanted to show the results in your textarea
                // you could do this (it might be a good idea to override the id
                // of the textarea as well the same way we did with the ddl):
                $('#EmailsSentTo').val(result);
            }
        });
    });
});