我使用 HttpGet 和 HttpPost 错了吗? ASP.NET C#
Am I using HttpGet and HttpPost wrong? ASP.NET C#
代码:
[HttpPost]
public ActionResult DeleteProduct(int ProductID, string Name, decimal Price, string Photo, int Likes, string Description, Enums.ProductCategory.Category Category)
{
ProductModel productModel = new ProductModel
{
ProductID = ProductID,
Name = Name,
Price = Price,
Photo = Photo,
Likes = Likes,
Description = Description,
Category = Category
};
return View(productModel);
}
[HttpGet]
public ActionResult DeleteProduct(int ProductID)
{
if(UserController.userSessionID != 0)
{
user.DeleteProduct(ProductID);
return RedirectToAction("ViewSellerProducts", "Product");
}
return View();
}
但我读到 HttpGet 只应该获取数据而不做任何其他事情,[HttpGet]DeleteProduct()
没有这样做。事实上,如果我将 DeleteProduct()
更改为 [HttpPost]
,则 ID 将为 0,那么我将如何在视图中获取 ID?
在页面设置产品时:
当我在显示所有详细信息的页面上并且必须单击删除时:
您缺少 input
产品 ID,无法将其作为 HttpPost
传递
<form asp-action="DeleteProduct">
<input type="hidden" asp-for="ProductID" />
<input type="submit" value="Delete" class="btn btn-danger" />
...
</form>
代码:
[HttpPost]
public ActionResult DeleteProduct(int ProductID, string Name, decimal Price, string Photo, int Likes, string Description, Enums.ProductCategory.Category Category)
{
ProductModel productModel = new ProductModel
{
ProductID = ProductID,
Name = Name,
Price = Price,
Photo = Photo,
Likes = Likes,
Description = Description,
Category = Category
};
return View(productModel);
}
[HttpGet]
public ActionResult DeleteProduct(int ProductID)
{
if(UserController.userSessionID != 0)
{
user.DeleteProduct(ProductID);
return RedirectToAction("ViewSellerProducts", "Product");
}
return View();
}
但我读到 HttpGet 只应该获取数据而不做任何其他事情,[HttpGet]DeleteProduct()
没有这样做。事实上,如果我将 DeleteProduct()
更改为 [HttpPost]
,则 ID 将为 0,那么我将如何在视图中获取 ID?
在页面设置产品时:
当我在显示所有详细信息的页面上并且必须单击删除时:
您缺少 input
产品 ID,无法将其作为 HttpPost
<form asp-action="DeleteProduct">
<input type="hidden" asp-for="ProductID" />
<input type="submit" value="Delete" class="btn btn-danger" />
...
</form>