public IActionResult Add([FromBody] Discount model) { if (ValidateDiscount(model) || model.DiscountId != 0) { return(BadRequest(model)); } var counter = 0; int rand; while (true) { if (counter++ == 10) { return(StatusCode(400)); } rand = new Random().Next(1, 9999); if (!_db.Discounts.Any(d => d.Code == rand)) { break; } } model.Code = rand; _db.AddDiscount(model); return(Redirect("/Discount/Index")); }