c# - 我想使用@Html.ActionLink Delete 删除数据库中的一行,但它不起作用。我究竟做错了什么?
问题描述
它可以写出 cv.name,它也可以连接到数据库。但是我在删除操作上做错了。
cshtml:
<body>
@{
foreach( var cv in Model.GetCVs( username))
{
<h3 style="color: #193367; "><b> Name: @cv.name</b></h3> // this works
<form action="" method="post">
<input type="submit" name="name" value="@cv.name" /> // this works
</form>
@Html.ActionLink("Delete","Delete",new{ id = @cv.id}) // this does not work
}
}
</body>
上面的标记在我的 cshtml 文件中,大部分都可以正常工作。
cshtml.cs:
[BindProperty]
public int id { get; set; }
public const string vv = "Data Source=Tables.sqlite3";
public static SqliteConnection Connection = new SqliteConnection(vv);
static AboutModel()
{
Connection.Open();
}
public struct CV
{
public string name;
public int id;
}
public ActionResult Delete(int? id)
{
using (var cmd = Connection.CreateCommand())
{
cmd.CommandText = "Delete from MyTable1 where id = @id;";
return RedirectToPage("About");
}
}
这是我的代码,cshtml.cs
该Delete
方法不起作用。我不知道如何正确使用此delete
操作。我不希望它回问你确定要删除它吗?我只希望如果用户单击删除按钮,它应该删除表中的该行。
解决方案
你有两个问题。首先,您需要在命令中添加一个参数以对应@id
于命令文本中的 。其次,更重要的是,您需要实际执行命令。现在你只是创建一个新的DbCommand
,分配CommandText
属性然后丢弃它。
public ActionResult Delete(int? id)
{
// handle bad request (id is null) somehow
if(!ModelState.IsValid)
return View();
using (var cmd = Connection.CreateCommand())
{
cmd.CommandText = "Delete from MyTable1 where id = @id";
// set command type Text (or StoredProcedure)
// Text is the default so not strictly necessary
cmd.CommandType = CommandType.Text;
// create new parameter
var para = cmd.CreateParameter();
para.Value = id.Value;
para.ParameterName = "@id";
para.DbType = DbType.Int32;
// add parameter to command
cmd.Parameters.Add(para);
// need to make sure connection is open
if (Connection.State == ConnectionState.Closed || Connection.State == ConnectionState.Broken)
Connection.Open();
// execute command
cmd.ExecuteNonQuery();
}
return RedirectToPage("About");
}
旁注:您真的不应该将连接对象保存在static
字段或属性中。您应该尽可能靠近实际需要连接的位置/时间创建、打开、关闭/处置。由于这是一个 Web 应用程序,如果您这样做,您将陷入困境。
public ActionResult Delete(int? id)
{
// handle bad request (id is null) somehow
if(!ModelState.IsValid)
return View();
// create connection when we need it
using (var conn = new SqliteConnection(vv))
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "Delete from MyTable1 where id = @id";
// set command type Text (or StoredProcedure)
// Text is the default so not strictly necessary
cmd.CommandType = CommandType.Text;
// create new parameter
var para = cmd.CreateParameter();
para.Value = id.Value;
para.ParameterName = "@id";
para.DbType = DbType.Int32;
// add parameter to command
cmd.Parameters.Add(para);
// open the connection
conn.Open();
// execute command
cmd.ExecuteNonQuery();
}
return RedirectToPage("About");
}
推荐阅读
- css - HostBinding 未绑定到 CSS 变量
- asp.net-mvc - Azure AD B2C 登录/设置有问题
- android-studio - 在 Android Studio 运行配置中更改检测类
- msbuild - MSBuild 选择部分 RecursiveDir
- c - '{' 标记之前的预期 ':'、','、';'、'}' 或 '__attribute__'
- python - 在 jupyter notebook 中使用 matplotlib 的“下载绘图”功能时图像失真
- c++ - send() 调用仅适用于套接字中的 telnet
- python - 如何从 Python 中的 Excel 工作表的每个选项卡中读取多个表?
- google-chrome - 柏树 | 由于跨源而进行登录测试的问题
- python - Python 嵌套字典问题