简体   繁体   English

使用AJAX的Html.RenderAction

[英]Html.RenderAction using AJAX

Is it possible to use HTMl.RenderAction using ajax to provide the parameter? 是否可以使用ajax来使用HTMl.RenderAction来提供参数?

I have a controller action like this 我有这样的控制器动作

[ChildActionOnly]
Public ActionResult EmployeeData(string id)
{
    Employee employee = new Employee();
    //do work to get data

    Return PartialView(employee);
}

The partial view is just a small table with some employee data (name, address, etc) 局部视图只是一个包含一些员工数据(名称,地址等)的小表

I then have a page with a drop down list of employees with the data field being the id needed for EmployeeData(string id) 然后我有一个页面,其中包含员工下拉列表,其中数据字段是EmployeeData所需的ID(字符串ID)

I would like to use ajax so when a employee is selected from a drop down list the EmployeeData partial view will appear below it without refreshing the page. 我想使用ajax,因此当从下拉列表中选择员工时,EmployeeData部分视图将显示在其下方而不刷新页面。 Then again if another employee is selected. 如果选择了另一名员工,则再次。

Though i am not sure how to do this, if it is possible. 虽然我不知道如何做到这一点,如果可能的话。


As was recommended here is what I have now. 正如我在这里推荐的那样。 (please don't mind that this is not the employee data example i mentioned above, that data is not ready in the DB and I have multiple areas that will do this same thing so I decided to work on this one today) (请不要介意这不是我上面提到的员工数据示例,数据在数据库中没有准备就绪,我有多个区域可以做同样的事情所以我决定今天就这个工作)

here is my the JS in my view 在我看来,这是我的JS

  $("#repList").change(function () {
    var id = $("#repList").val();
    $.ajax({
        url: '/Reporting/GetSalesTargets/' + id,
        success: function (result) {

              $("#partialdiv").html(result);
        },
        error: function () {
            alert("error");
            }
    });
});

I am getting to the controller action that will return the view, here is it. 我正在进入将返回视图的控制器操作,这是它。

public ActionResult GetSalesTargets(string id)
{
    int month = DateTime.Now.Month;
    SalesMarketingReportingService mktSrv = new SalesMarketingReportingService();

    SalesTargetModel model = mktSrv.GetRSMSalesTargetModel(id, month);

    return PartialView(model);
}

It is possible but you have to remove the [ChildActionOnly] attribute. 这是可能的,但您必须删除[ChildActionOnly]属性。 The it becomes a normal action that returns a partial view the you could invoke using AJAX: 它成为一个普通的动作,返回你可以使用AJAX调用的局部视图:

$.ajax({
    url: '/home/employeedata/123',
    success: function(result) {
        $('#somedivid').html(result);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM