1

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.

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for response. I am passing something like ViewModel to my view, so I can access BuildingId inside foreach where I iterate through Model. So the id I want, is accessed like this: @employee.Office.BuildingId. I tryed your first approach, because I don't know how to pass it as Model property in my scenario.
So I am storing BuildingId inside input in each iteration: <input id="Id" type="hidden" value="@employee.Office.BuildingId" /> but I can only access first value from whole iteration like that. My goal is when I click on a button then BuildingDetails of specific Building which hosts Office of specific Employee appears. Atm It only works when I click on first button to show BuildingDetails. Hope you got what i mean.
Oh I see. If there are multiple Ids, you can create a function taking the id as a parameter and call it on your #LoadBuildingDetail click.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.