It can write out the cv.name, it also connects to the database. But I'm doing something wrong with a delete action.
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>
The markup above is in my cshtml file and the most of it is working all right.
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");
}
}
This is the code in my cshtml.cs and the Delete method is not working. I don't know how to properly use this delete action. I don't want that it asks back that are you sure want to delete it? I just want that if the user click on the delete button, it should delete that row in a table.
You have two problems. First, you need to add a parameter to your command to correspond to the
@idin the command text. Second, and more importantly, you need to actually execute the command. Right now you are just creating a newDbCommand, assigning theCommandTextproperty and then discarding it.Side note: you really shouldn't hold your connection object in a
staticfield or property. You should create, open, close/dispose as close as possible to where/when the connection is actually needed. Since this is a web application, you are going to get yourself into trouble if you do it this way.