清空剃刀页面检查 id 是否存在
Empty razor page check if id exist
对于我自己的项目,我希望能够删除提示文章,这可行。但我知道我有一个问题,它也会删除不存在的提示文章。有人知道如何解决这个问题吗?
提示文章管理器中的代码:
*//Delete tip article in db
public bool DeleteTipArticle(int articleNumber)
{
FillTipArticleList();
return tipArticleDB.DeleteDb(articleNumber);
}*
数据库中的代码:
//Delete the tip article for the tiparticle database
public bool DeleteDb(int articleNumber)
{
MySqlConnection connection = new MySqlConnection(databaseConnection);
//Sql string for deleting the tip article
string sql = "DELETE FROM tiparticle WHERE ArticleNumber = @ArticleNumber";
MySqlCommand command = new MySqlCommand(sql, connection);
command.Parameters.AddWithValue("@ArticleNumber", articleNumber);
try
{
connection.Open();
int amount = command.ExecuteNonQuery();
return amount > 0;
}
catch (MySqlException ex)
{
return false;
}
catch (FormatException ex)
{
return false;
}
catch (NullReferenceException ex)
{
return false;
}
finally
{
if (connection.State == System.Data.ConnectionState.Open)
{
connection.Close();
}
}
}
razor 页面背后的代码:
[Authorize(Roles = "admin")]
public class DeleteTipArticleModel : PageModel
{
[BindProperty]
public int articleNumber { get; set; }
public void OnGet()
{
}
public IActionResult OnPost()
{
if (ModelState.IsValid)
{
string message = "The tip article has been deleted!";
ViewData["Message"] = message;
TipArticleManager deleteToTipArticleDatabase = new TipArticleManager();
deleteToTipArticleDatabase.DeleteTipArticle(articleNumber);
return Page();
}
else
{
ViewData["Message"] = "Please enter all data fields";
return Page();
}
}
}
我想我必须对返回的金额 > 0 做些什么,但我不确定在哪里以及如何做。
您可以检查 bool succeeded = deleteToTipArticleDatabase.DeleteTipArticle(articleNumber);
的 return 值以检查金额 > 0。
[Authorize(Roles = "admin")]
public class DeleteTipArticleModel : PageModel
{
[BindProperty]
public int articleNumber { get; set; }
public void OnGet()
{
}
public IActionResult OnPost()
{
if (ModelState.IsValid)
{
TipArticleManager deleteToTipArticleDatabase = new TipArticleManager();
bool succeeded = DeleteToTipArticleDatabase.DeleteTipArticle(articleNumber);
if(succeeded)
{
string message = "The tip article has been deleted!";
ViewData["Message"] = message;
}
return Page();
}
else
{
ViewData["Message"] = "Please enter all data fields";
return Page();
}
}
}
否则,您可以调用数据库来获取文章编号,看看它是否已经存在。
对于我自己的项目,我希望能够删除提示文章,这可行。但我知道我有一个问题,它也会删除不存在的提示文章。有人知道如何解决这个问题吗?
提示文章管理器中的代码:
*//Delete tip article in db
public bool DeleteTipArticle(int articleNumber)
{
FillTipArticleList();
return tipArticleDB.DeleteDb(articleNumber);
}*
数据库中的代码:
//Delete the tip article for the tiparticle database
public bool DeleteDb(int articleNumber)
{
MySqlConnection connection = new MySqlConnection(databaseConnection);
//Sql string for deleting the tip article
string sql = "DELETE FROM tiparticle WHERE ArticleNumber = @ArticleNumber";
MySqlCommand command = new MySqlCommand(sql, connection);
command.Parameters.AddWithValue("@ArticleNumber", articleNumber);
try
{
connection.Open();
int amount = command.ExecuteNonQuery();
return amount > 0;
}
catch (MySqlException ex)
{
return false;
}
catch (FormatException ex)
{
return false;
}
catch (NullReferenceException ex)
{
return false;
}
finally
{
if (connection.State == System.Data.ConnectionState.Open)
{
connection.Close();
}
}
}
razor 页面背后的代码:
[Authorize(Roles = "admin")]
public class DeleteTipArticleModel : PageModel
{
[BindProperty]
public int articleNumber { get; set; }
public void OnGet()
{
}
public IActionResult OnPost()
{
if (ModelState.IsValid)
{
string message = "The tip article has been deleted!";
ViewData["Message"] = message;
TipArticleManager deleteToTipArticleDatabase = new TipArticleManager();
deleteToTipArticleDatabase.DeleteTipArticle(articleNumber);
return Page();
}
else
{
ViewData["Message"] = "Please enter all data fields";
return Page();
}
}
}
我想我必须对返回的金额 > 0 做些什么,但我不确定在哪里以及如何做。
您可以检查 bool succeeded = deleteToTipArticleDatabase.DeleteTipArticle(articleNumber);
的 return 值以检查金额 > 0。
[Authorize(Roles = "admin")]
public class DeleteTipArticleModel : PageModel
{
[BindProperty]
public int articleNumber { get; set; }
public void OnGet()
{
}
public IActionResult OnPost()
{
if (ModelState.IsValid)
{
TipArticleManager deleteToTipArticleDatabase = new TipArticleManager();
bool succeeded = DeleteToTipArticleDatabase.DeleteTipArticle(articleNumber);
if(succeeded)
{
string message = "The tip article has been deleted!";
ViewData["Message"] = message;
}
return Page();
}
else
{
ViewData["Message"] = "Please enter all data fields";
return Page();
}
}
}
否则,您可以调用数据库来获取文章编号,看看它是否已经存在。