Nashmár
Nashmár

Reputation: 392

Pass parameter to jQuery load function in ASP.NET Core

I would like to pass an id parameter from this action in controller

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

into this load method in view to run AJAX.

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

I am new to jQuery, but I guess I need to store id vaule somehow before passing it into load function, so controller/action/parameter approach does not work. But atm I had no luck.

Upvotes: 1

Views: 3943

Answers (1)

Soham
Soham

Reputation: 1281

If you want to pass id to the controller via jquery load method, you can pass it directly into the load method as an object.

I assume you have the id's value somewhere on the page or you are hardcoding it in the view using razor syntax from your model.

In any case, try using something like this in your jquery

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

or

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

Reference: jQuery load method

Upvotes: 3

Related Questions