Вы можете вернуть PartialView из контроллера и показать его в функции успеха ajax. Ниже приведена простая демонстрация, к которой можно обратиться:
Модель продукта:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public int Price { get; set; }
}
Основной вид:
@model IEnumerable<Product>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
<button class="btnAdd" onclick="GetProduct(@item.Id)">Add To Cart</button>
</td>
</tr>
}
</tbody>
</table>
<div>
<label>ProductCart</label>
<table id="table">
<thead>
<tr>
<th>
Name
</th>
<th>
Price
</th>
<th></th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
@section Scripts
{
<script type="text/javascript">
function GetProduct(id) {
$.ajax({
type: "get",
url: "/home/getpartial?id="+id,
success: function (result) {
console.log(result);
$("#table tbody").append(result);
}
});
}
</script>
}
PartialView:
@model Product
<tr>
<td>
@Html.DisplayFor(model => model.Name)
</td>
<td>
@Html.DisplayFor(model => model.Price)
</td>
</tr>
Контроллер:
public async Task<IActionResult> Products()
{
var products =await _context.Products.ToListAsync();
return View(products);
}
public IActionResult GetPartial(int id)
{
var model = _context.Products.Find(id);
return PartialView("_ItemPartial", model);
}
Результат:
![enter image description here](https://i.stack.imgur.com/NpGw2.gif)