Hi @Coreysan,
You can set the data-id to Model.ProductId and it will get the id of the clicked anchor and display the corresponding data in the mode (detailed view) on the screen.
<a href="#" id="btnEdit" class="btn btn-primary btn-sm" data-id="@item.ProductId">Edit</a>
Edit
View
Index
@model List<CoreMvc.Models.Product>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(function () {
$("[id*=BtnEdit]").click(function () {
var productId = $(this).attr('data-id');
$.ajax({
type: 'POST',
url: '/Home/AddToCart',
data: { id: productId }
}).done(function (response) {
$("#actionCon").html(response);
$("#myModalEdit").modal('show');
}).fail(function (XMLHttpRequest, textStatus, errorThrown) {
debugger;
alert("Fail");
});
});
});
</script>
</head>
<body>
<table class="table table-striped">
<thead>
<tr>
<td>Id</td>
<td>Name</td>
<td>Data</td>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@item.ProductId</td>
<td>@item.Name</td>
<td>@item.Data</td>
<td>
<button type="button" id="BtnEdit" data-id="@item.ProductId">AddToCart</button>
</td>
</tr>
}
</tbody>
</table>
<div id="actionCon"></div>
</body>
</html>
AddToCart
@model CoreMvc.Models.Product
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<form id="EditCat">
<div class="container">
<!-- The Modal -->
<div class="modal fade" id="myModalEdit">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Edit Category</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<input id="hfId" type="hidden" name="ID" value="@Model.ProductId" />
<div class="form-group">
<input type="text" value="@Model.Name" class="form-control" id="txtName" name="Name" placeholder="Enter Name" />
</div>
<br />
<div class="form-group">
<input type="text" value="@Model.Data" class="form-control" id="txtData" name="Name" placeholder="Enter Country" />
</div>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<a asp-controller="Home" asp-action="Add" asp-route-id="@Model.ProductId">Add</a>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</form>
Model
public class Product
{
public int ProductId { set; get; }
public string Name { set; get; }
public string Data { set; get; }
}
public IActionResult Index()
{
List<Product> Products = GetProducts(null);
return View(Products);
}
[HttpPost]
public IActionResult AddToCart(int id)
{
Product product = new Product();
product.ProductId = id;
product.Name = GetProducts(id).FirstOrDefault().Name;
product.Data = GetProducts(id).FirstOrDefault().Data;
return PartialView(product);
}
[HttpGet("AddToCart/{id}")]
public IActionResult Add([FromRoute] int id)
{
Product product = new Product();
product.ProductId = id;
//....
return Json(product);
}
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.