C# 无法更新或编辑HTTP post方法中的特定数据
我想更新数量值。为此,我使用更新数量索引的HTTPPost方法。但由于错误,它无法运行。 这是我的密码:C# 无法更新或编辑HTTP post方法中的特定数据,c#,asp.net-core,asp.net-core-mvc,C#,Asp.net Core,Asp.net Core Mvc,我想更新数量值。为此,我使用更新数量索引的HTTPPost方法。但由于错误,它无法运行。 这是我的密码: public IActionResult Index() { return View(_db.Shop.Include(c => c.Category).Include(f => f.SubCategory).ToList()); } [HttpPost] publi
public IActionResult Index()
{
return View(_db.Shop.Include(c => c.Category).Include(f => f.SubCategory).ToList());
}
[HttpPost]
public IActionResult Index(int? id)
{
var product = _db.Shop.Where(x => x.Id == id).FirstOrDefault();
IndexVm ji = new IndexVm
{
Id = product.Id,
Image = product.Image,
Image1 = product.Image1,
Quantity = product.Quantity++,
Price = product.Price,
PreviousPrice = product.PreviousPrice,
Description = product.Description,
};
_db.Shop.Update(ji);
_db.SaveChanges();
return RedirectToAction(nameof(Index));
}
Index.cshtml
@using Amazon.Models
@model IEnumerable<Shop>
@{
ViewData["Title"] = "Index";
}
<br /><br />
<div class="row">
<div class="col-6">
<h2 class="text-info">Product List</h2>
</div>
<div class="col-6 text-right">
<a asp-action="Create" class="btn btn-info"> Add New Product </a>
</div>
</div>
<form asp-action="Create" method="post" enctype="multipart/form-data">
<div>
<table class="table table-striped border" id="myTable">
<thead>
<tr class="table-info">
<th>
@Html.DisplayNameFor(c => c.Name)
</th>
<th>
@Html.DisplayNameFor(c => c.Price)
</th>
<th>
@Html.DisplayNameFor(c => c.PreviousPrice)
</th>
<th>
@Html.DisplayNameFor(c => c.Quantity)
</th>
<th>
@Html.DisplayNameFor(c => c.Description)
</th>
<th>
@Html.DisplayNameFor(c => c.Quantity)
</th>
<th>
@Html.DisplayNameFor(c => c.CategoryTypeId)
</th>
<th>
@Html.DisplayNameFor(c => c.SubCategoryTypeId)
</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr class="text-info">
<td> @item.Name </td>
<td> @item.Price </td>
<td> @item.PreviousPrice </td>
<td>@item.Quantity</td>
<td>
<partial name="_QuantityPartial" model="@item.Id" />
</td>
<td> @item.Description </td>
<td> @item.Size </td>
<td> @item.Category.CategoryName </td>
<td> @item.SubCategory.SubCategoryName </td>
<td>
<partial name="_DeletePartial" model="@item.Id" />
</td>
</tr>
}
</tbody>
</table>
</div>
</form>
<br /> <br />
@section scripts{
<script type="text/javascript">
$(document).ready(function () {
$('#myTable').DataTable();
});
</script>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script>
function incrementValue(e) {
e.preventDefault();
var fieldName = $(e.target).data('field');
var parent = $(e.target).closest('div');
var currentVal = parseInt(parent.find('input[name=' + fieldName + ']').val(), 10);
if (!isNaN(currentVal)) {
parent.find('input[name=' + fieldName + ']').val(currentVal + 1);
} else {
parent.find('input[name=' + fieldName + ']').val(0);
}
}
function decrementValue(e) {
e.preventDefault();
var fieldName = $(e.target).data('field');
var parent = $(e.target).closest('div');
var currentVal = parseInt(parent.find('input[name=' + fieldName + ']').val(), 10);
if (!isNaN(currentVal) && currentVal > 0) {
parent.find('input[name=' + fieldName + ']').val(currentVal - 1);
} else {
parent.find('input[name=' + fieldName + ']').val(0);
}
}
$('.input-group').on('click', '.button-plus', function (e) {
incrementValue(e);
});
$('.input-group').on('click', '.button-minus', function (e) {
decrementValue(e);
});
</script>
}
@使用Amazon.Models
@模型IEnumerable
@{
ViewData[“标题”]=“索引”;
}
产品清单
我是初学者,请任何人帮助。您应该使用:
var product = _db.Shop.AsNoTracking().Where(x => x.Id == id).FirstOrDefault();
您正在获取产品,但正在更新具有相同id的新实例ji。由于id相同,EF认为这两个对象具有相同的主键,因此不应发生错误
您的另一个选项是更新从数据库获取的产品:
[HttpPost]
public IActionResult Index(int? id)
{
var product = _db.Shop.Where(x => x.Id == id).FirstOrDefault();
product.Quantity++;
_db.Shop.Update(product);
_db.SaveChanges();
return RedirectToAction(nameof(Index));
}
您可以在此处找到更多信息:您应该使用:
var product = _db.Shop.AsNoTracking().Where(x => x.Id == id).FirstOrDefault();
您正在获取产品,但正在更新具有相同id的新实例ji。由于id相同,EF认为这两个对象具有相同的主键,因此不应发生错误
您的另一个选项是更新从数据库获取的产品:
[HttpPost]
public IActionResult Index(int? id)
{
var product = _db.Shop.Where(x => x.Id == id).FirstOrDefault();
product.Quantity++;
_db.Shop.Update(product);
_db.SaveChanges();
return RedirectToAction(nameof(Index));
}
您可以在此处找到更多信息:您犯了以下两个错误:
您正在将VM传递给更新函数,这是错误的
要更新任何现有EF对象,必须更新该对象,而不是创建新对象
{
var product = _db.Shop.Where(x => x.Id == id).FirstOrDefault();
product.Quantity++;
_db.Shop.Update(product);
_db.SaveChanges();
return RedirectToAction(nameof(Index));
}
你犯了以下两个错误:
您正在将VM传递给更新函数,这是错误的
要更新任何现有EF对象,必须更新该对象,而不是创建新对象
{
var product = _db.Shop.Where(x => x.Id == id).FirstOrDefault();
product.Quantity++;
_db.Shop.Update(product);
_db.SaveChanges();
return RedirectToAction(nameof(Index));
}
实体商店有问题。请在此处发布您的实体,可能会重新格式化您的Index.cshtml
,并发布文件的标题。@Longlong我发布了带有标题文件的Index.cshtml。您的实体商店
有问题。请将您的实体发布在此处,并可能重新格式化您的Index.cshtml
,同时发布文件的标题。@Longlong我发布了带有标题文件的Index.cshtml。