如何检查 POST 请求中的浮点值是否为空
How to check if a float value is empty in a POST request
我想知道当我发送浮点值时我应该如何检查 POST 请求是否发送数据,我尝试使用 if 语句对我在 [=23] 中给出的所有值执行此操作=] 请求,但我不能对浮点值使用 if 语句,因为它不能为空。这是我的 post 请求。对于大多数人来说,它看起来真的很难看,但我不熟悉 ASP.NET MVC
[HttpPut("Product/Edit/{productid}")]
public IActionResult ProductEdit(string token, int productid, [FromBody]Product p)
{
bool RoleId = JWTValidator.RoleIDTokenValidation(token);
var edit = _context.products.Find(productid);
if (RoleId)
{
if (p.Name != null)
{
edit.Name = p.Name;
}
else
{
edit.Name = edit.Name;
}
if (p.Description != null)
{
edit.Description = p.Description;
}
else
{
edit.Description = edit.Description;
}
edit.Price = p.Price;
if (p.FirstImg != null)
{
edit.FirstImg = p.FirstImg;
}
else
{
edit.FirstImg = edit.FirstImg;
}
edit.Stock = p.Stock;
if (ModelState.IsValid)
{
_context.products.Update(edit);
_context.SaveChanges();
}
else
{
return BadRequest(ModelState);
}
return Ok("Product updated");
}
return Unauthorized();
}
价格是浮动的,股票是整数,所以我尝试像我对其他人所做的那样用 if 语句来做,但由于上述原因无法完成。我尝试了这个(代码在文本下方剪断)但是当我这样做并发送 POST 请求时,我的数据库将更新为价格和库存的空值。
if(p.Price.ToString() != null)
{
edit.Price = p.Price;
} else {
edit.Price = edit.Price;
}
所有这些的 if 语句检查通过 post 请求发送的值是否为空,否则将保留数据库中的原始值。
任何帮助或建议将不胜感激,我很高兴了解我是一名学生,所以任何评论都可以。
正如您提到的价格是浮动类型,因此默认情况下它将是 0.0f(如果您第一次没有保存任何值)所以只需与价格 > 0
进行比较
像
一样更改您的代码
if(p.Price>0)
{
edit.Price = p.Price;
} else {
edit.Price = edit.Price;
}
正如 Taskin 所说,价格是浮点型的,因此默认情况下为 0。
所以请尝试像下面这样设置您的价格。
edit.Price = p.Price != edit.Price ? p.Price : edit.Price;
你可以这样做。
if (p.Price.Equals(null))
{
edit.Price = edit.Price;
}
else
{
edit.Price = p.Price;
}
我想知道当我发送浮点值时我应该如何检查 POST 请求是否发送数据,我尝试使用 if 语句对我在 [=23] 中给出的所有值执行此操作=] 请求,但我不能对浮点值使用 if 语句,因为它不能为空。这是我的 post 请求。对于大多数人来说,它看起来真的很难看,但我不熟悉 ASP.NET MVC
[HttpPut("Product/Edit/{productid}")]
public IActionResult ProductEdit(string token, int productid, [FromBody]Product p)
{
bool RoleId = JWTValidator.RoleIDTokenValidation(token);
var edit = _context.products.Find(productid);
if (RoleId)
{
if (p.Name != null)
{
edit.Name = p.Name;
}
else
{
edit.Name = edit.Name;
}
if (p.Description != null)
{
edit.Description = p.Description;
}
else
{
edit.Description = edit.Description;
}
edit.Price = p.Price;
if (p.FirstImg != null)
{
edit.FirstImg = p.FirstImg;
}
else
{
edit.FirstImg = edit.FirstImg;
}
edit.Stock = p.Stock;
if (ModelState.IsValid)
{
_context.products.Update(edit);
_context.SaveChanges();
}
else
{
return BadRequest(ModelState);
}
return Ok("Product updated");
}
return Unauthorized();
}
价格是浮动的,股票是整数,所以我尝试像我对其他人所做的那样用 if 语句来做,但由于上述原因无法完成。我尝试了这个(代码在文本下方剪断)但是当我这样做并发送 POST 请求时,我的数据库将更新为价格和库存的空值。
if(p.Price.ToString() != null)
{
edit.Price = p.Price;
} else {
edit.Price = edit.Price;
}
所有这些的 if 语句检查通过 post 请求发送的值是否为空,否则将保留数据库中的原始值。
任何帮助或建议将不胜感激,我很高兴了解我是一名学生,所以任何评论都可以。
正如您提到的价格是浮动类型,因此默认情况下它将是 0.0f(如果您第一次没有保存任何值)所以只需与价格 > 0
进行比较像
一样更改您的代码if(p.Price>0)
{
edit.Price = p.Price;
} else {
edit.Price = edit.Price;
}
正如 Taskin 所说,价格是浮点型的,因此默认情况下为 0。 所以请尝试像下面这样设置您的价格。
edit.Price = p.Price != edit.Price ? p.Price : edit.Price;
你可以这样做。
if (p.Price.Equals(null))
{
edit.Price = edit.Price;
}
else
{
edit.Price = p.Price;
}