I know how to render a partial view with jquery.
Now, I am trying to render different content in the same div element each time the user click (using partial views).
I have a method in my home controller
public IActionResult Details(int x)
{
Blog A = new Blog() { Id = 1};
Blog B = new Blog() { Id = 2};
if (B.Id == x)
return PartialView("_blogPartial", B);
return PartialView("_blogPartial", A);
}
I am rendering this partial view on click.
$(document).ready(function(){
// For button 1
$("button1").click(function () {
$("#partial").load('@Url.Action("Details", new { x = 1})');
});
// For button 2
$("#button2").click(function () {
$("#partial").load('@Url.Action("Details",new { x=2})');
});
});
And below are the two buttons .
<button id="button1">BlogA</button>
<button id="button2">BlogB</button>
<div id="partial">
</div>
The problem: If there are 'n' buttons I have to write click function 'n' times for each button. Is there any way that I can pass my button id to the Url.Action() method which will pass it to the Details method.
I need to do something like
$("button").click(function () {
$("#partial").load('@Url.Action("Details", new { x = buttonid })');
});