使用Jquery Ajax调用MVC4局部视图

使用Jquery Ajax调用MVC4局部视图,ajax,asp.net-mvc,jquery,asp.net-mvc-4,Ajax,Asp.net Mvc,Jquery,Asp.net Mvc 4,我需要你帮我完成mvc4项目。我有两个下拉菜单。当我选择第一个时,第二个将在Jquery和Ajax的帮助下自动填充。当我选择另一个时,它现在填充了一些数据,我需要调用一个调用PL/SQL过程的方法,我需要向该方法传递一个值,该值在第二个下拉菜单中选择。 该方法返回一些我需要传递到局部视图的数据,在该局部视图中,我需要从传递的数据生成树视图。 到目前为止,当我硬编码一个值并从Controller调用我的方法时,我能够在局部视图中使用jsTree生成TreeView,但当我从第二个下拉列表中选择一个

我需要你帮我完成mvc4项目。我有两个下拉菜单。当我选择第一个时,第二个将在Jquery和Ajax的帮助下自动填充。当我选择另一个时,它现在填充了一些数据,我需要调用一个调用PL/SQL过程的方法,我需要向该方法传递一个值,该值在第二个下拉菜单中选择。 该方法返回一些我需要传递到局部视图的数据,在该局部视图中,我需要从传递的数据生成树视图。 到目前为止,当我硬编码一个值并从Controller调用我的方法时,我能够在局部视图中使用jsTree生成TreeView,但当我从第二个下拉列表中选择一个值时,我需要这样做

这是我的代码:

我的控制器

    public class IndexController : Controller
{

    public ActionResult Index()
    {
        EpfeSelectScreen model = new EpfeSelectScreen();

        #region Country

        var b = (from a in dbEntitiesErste.CONFIG_COUNTRIES
                 orderby a.COUNTRY_ID
                 select new Countries
                 {
                     Text = a.COUNTRY_NAME,
                     Value = a.COUNTRY_ID
                 });

        model.Country = b.OrderBy(x => x.Text).ToList();
        #endregion

        #region Oracle Stored Procedures

        List<TreeNode> list = new List<TreeNode>();
        list = ClassData.GetAllClasses(1); //hardcoded value 1 Here goes the value from second drop down list
        var TopHierarchy = list.Where(x => x.ParentId == -1).FirstOrDefault();
        SetChildren(TopHierarchy, list);

        #endregion

        var pmViewModel = new MainViewModel
        {
            FullModelObject = model,
            PartialModelObject = TopHierarchy
        };

        return View(pmViewModel);


    }

    #region generate Tree
    private void SetChildren(TreeNode model, List<TreeNode> list)
    {
        var childs = list.Where(x => x.ParentId == model.ChildId).ToList();
        if (childs.Count > 0)
        {
            foreach (var child in childs)
            {
                SetChildren(child, list);
                model.Children.Add(child);

            }
        }

    }
    #endregion


    #region jquery methods

    [OutputCache(Duration = 0)]
    [HttpGet]
    public JsonResult Application(string[] Country)
    {
        var apl = new List<Applications>();
        if (Country[0] == "")
        {
            //*aplications
            var result = (from a in dbEntitiesErste.CONFIG_APPLICATIONS
                             select new Applications
                             {
                                 Text = a.APPLICATION_NAME,
                                 Value = a.APPLICATION_ID
                             });//*.OrderBy(x => x.Text).ToList()

            apl.Add(new Applications { Value = 0, Text = "--Please choose application--" });
            apl.AddRange(result.OrderBy(x => x.Text).ToList());
        }
        else
        {
            var result = (from a in dbEntitiesErste.CONFIG_APPLICATIONS
                             where Country.Contains(a.COUNTRY_ID)
                             select new Applications
                             {
                                 Text = a.APPLICATION_NAME,
                                 Value = a.APPLICATION_ID
                             }); //*.OrderBy(x => x.Text).ToList();
            apl.Add(new Applications { Value = 0, Text = "--Please choose application--" });
            apl.AddRange(result.OrderBy(x => x.Text).ToList());
        }

        var retVal = new { Application = aplikacije };
        return Json(retVal, JsonRequestBehavior.AllowGet);
    }

    //[OutputCache(Duration = 0)]
    //[HttpGet]
    //public JsonResult Tree(int idApp)
    //{
    //    var ret = (from a in dbEntitiesErste.CONFIG_APPLICATIONS
    //               select new Applications
    //               {
    //                   Text = a.APPLICATION_NAME,
    //                   Value = a.APPLICATION_ID
    //               }).OrderBy(x => x.Text).ToList();

    //    return Json(ret, JsonRequestBehavior.AllowGet);
    //}


    #endregion
}
最后一个呢

public class TreeNode
{
    public int ParentId { get; set; }
    public int ChildId { get; set; }
    public int ObjectRelId { get; set; }
    public string ObjectIdentifier { get; set; }
    public string ObjectTypeId { get; set; }
    public IList<TreeNode> Children { get; set; }

    public TreeNode()
    {
        Children = new List<TreeNode>();
    }

}
这些是我的纸条

$(document).ready(function () {
$("#Country").on('change', CountryChange);
//$("#selectedApplication").on('change', ApplicationChange);
//*
$("#Application").on('change', ApplicationChange);
});


function CountryChange() {

var CountryIds = [];
$("#Country option:selected").each(function (i, selected) {
    CountryIds[i] = $(selected).val();
});

$.ajax({
    url: pathAplications,
    type: 'GET',
    data: { Country: CountryIds },
    success: function (data) {
        var apl = $("#Application");

        apl.empty();

        for (var j = 0; j < data.Application.length; j++) {
            var item = data.Application[j];
            var option = "<option value='" + item.Value + "'>" + item.Text + "</option>";
            apl.append(option);
        }

    },
    error: function (x, y) {
        $("#displayError").html(x.val());

    },
    traditional: true
});
}


function ApplicationChange() {

var applicationId = [];
$("#Application option:selected").each(function (i, selected) {
    applicationId[i] = $(selected).val();
});

$.ajax({
    url: pathTreeView,
    type: 'GET',
    data: { Application: applicationId },
    success: function (data) {
        var tree = $("#selectedApplication");

        trees.empty();

        for (var j = 0; j < data.Application.length; j++) {
            var item = data.Application[j];
            var option = "<option value='" + item.Value + "'>" + item.Text + "</option>";
            trees.append(option);
        }

    },
    error: function (x, y) {
        $("#displayError").html(x.val());

    },
    traditional: true
});
}

函数ApplicationChange捕捉到了正确的值,但我不知道如何使用它调用我的pl/sql过程方法并将数据返回到部分视图。

像这样给出url

'@Url.Action("actionname","controllerNmae")',
 $.getJSON('@Url.Action("Controller Name here")',
 function ()
 {          
 });
还要确保如果您正在发出get或post请求,那么目标操作也应该具有该属性

此外,您的数据类型将是json

或者像这样更好地使用

'@Url.Action("actionname","controllerNmae")',
 $.getJSON('@Url.Action("Controller Name here")',
 function ()
 {          
 });

像这样给出你的url

'@Url.Action("actionname","controllerNmae")',
 $.getJSON('@Url.Action("Controller Name here")',
 function ()
 {          
 });
还要确保如果您正在发出get或post请求,那么目标操作也应该具有该属性

此外,您的数据类型将是json

或者像这样更好地使用

'@Url.Action("actionname","controllerNmae")',
 $.getJSON('@Url.Action("Controller Name here")',
 function ()
 {          
 });

很抱歉,我对Ajax是新手。我不知道怎么做。如果代码出现在.js页面中,我们还可以使用以下url:window.location+Controller/Action。因为MVC解析器在那里不起作用。很抱歉,我对Ajax不熟悉。我不知道怎么做。如果代码出现在.js页面中,我们还可以使用以下url:window.location+Controller/Action。因为MVC解析器在那里不起作用。