首页 > 解决方案 > 将参数传递给 ASP.NET Core 中的 jQuery 加载函数

问题描述

我想id在控制器中传递这个动作的参数

public IActionResult BuildingDetail(int id)
{
  return PartialView("_BuildingDetailsPartial", _buildingRepository.GetById(id));
}

进入这个load方法是为了运行AJAX。

@section Scripts{
<script type="text/javascript">
    $(document).ready(function () {
        $("#LoadBuildingDetail").click(function () {            
            $("#BuildingDetail").load("/employees/buildingdetail/id");              
        });
    })
</script>}

我是 jQuery 的新手,但我想我需要在将idvaule 传递给加载函数之前以某种方式存储它,所以controller/action/parameter方法不起作用。但是atm我没有运气。

标签: c#jqueryasp.netasp.net-core

解决方案


如果你想通过 jquery 方法将 id 传递给控制器​​,你可以将它作为对象load直接传递给方法。load

我假设您在页面上的某处有 id 的值,或者您正在使用模型中的剃刀语法在视图中对其进行硬编码。

无论如何,请尝试在您的 jquery 中使用类似的东西

//passing id's value from the control on the page
$("#BuildingDetail").load("/employees/buildingdetail", { id: $("#Id").val() });

或者

//passing id's value from the Model property
$("#BuildingDetail").load("/employees/buildingdetail", { id: @Model.Id });

参考:jQuery加载方法


推荐阅读