是否可以使用 HTMl.RenderAction 使用 ajax 提供参数?
我有一个这样的 Controller 操作
[ChildActionOnly]
Public ActionResult EmployeeData(string id)
{
Employee employee = new Employee();
//do work to get data
Return PartialView(employee);
}
部分 View 只是一个包含一些员工数据(姓名、地址等)的小表
然后我有一个页面,其中包含员工的下拉列表,数据字段是 EmployeeData(string id) 所需的 id
我想使用 ajax,因此当从下拉列表中选择员工时,EmployeeData 部分 View 将显示在其下方而不刷新页面。如果选择了另一名员工,则再次进行。
虽然我不确定如何做到这一点,如果可能的话。
这里推荐的就是我现在拥有的。 (请不要介意这不是我上面提到的员工数据示例,数据库中的数据还没有准备好,我有多个区域会做同样的事情所以我今天决定处理这个)
这是我认为的 JS
$("#repList").change(function () {
var id = $("#repList").val();
$.ajax({
url: '/Reporting/GetSalesTargets/' + id,
success: function (result) {
$("#partialdiv").html(result);
},
error: function () {
alert("error");
}
});
});
我正在处理将返回 View 的 Controller 操作,就在这里。
public ActionResult GetSalesTargets(string id)
{
int month = DateTime.Now.Month;
SalesMarketingReportingService mktSrv = new SalesMarketingReportingService();
SalesTargetModel model = mktSrv.GetRSMSalesTargetModel(id, month);
return PartialView(model);
}
最佳答案
这是可能的,但您必须删除 [ChildActionOnly]
属性。它变成了返回局部 View 的正常操作,您可以使用 AJAX 调用它:
$.ajax({
url: '/home/employeedata/123',
success: function(result) {
$('#somedivid').html(result);
}
});
关于c# - Html.RenderAction 使用 AJAX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3703364/