首页 > 解决方案 > 我想使用@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.csDelete方法不起作用。我不知道如何正确使用此delete操作。我不希望它回问你确定要删除它吗?我只希望如果用户单击删除按钮,它应该删除表中的该行。

标签: c#htmlrazorhtml.actionlink

解决方案


你有两个问题。首先,您需要在命令中添加一个参数以对应@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");
}

推荐阅读