C# 我想用@Html.ActionLink删除数据库中的一行,但它不起作用。我做错了什么?

C# 我想用@Html.ActionLink删除数据库中的一行,但它不起作用。我做错了什么?,c#,html,razor,html.actionlink,C#,Html,Razor,Html.actionlink,它可以写出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=""

它可以写出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.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");
}
旁注:您不应该将连接对象保存在
静态字段或属性中。您应该尽可能靠近实际需要连接的位置/时间创建、打开、关闭/处置连接。由于这是一个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");
}

我认为@id命令参数有问题。您调试了删除操作吗?
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");
}