我正在努力了解3层架构,我遇到了一个问题.我知道如何将数据插入到我的数据库中,但我不知道如何通过按下按钮来删除表内的所有信息.我的DAL。
namespace DAL
{
public class ClsDataLayer
{
SqlConnection SqlConn = new SqlConnection("Data Source = (localdb)\\mssqllocaldb; Initial Catalog = Testdatabase; Integrated Security = True");
public List<Gebruiker> Testlijst()
{
string commandText_Testlijst = "Select ID, Locatie,DatumTijd from Userinfo2";
List<Gebruiker> Gebruikers = new List<Gebruiker>();
using (SqlConn)
{
SqlConn.Open();
using (SqlCommand cmd = new SqlCommand(commandText_Testlijst, SqlConn))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var gebruiker = new Gebruiker
{
ID = reader.GetInt64(0),
Locatie = reader.GetString(1),
DatumTijd = reader.GetDateTime(2)
};
Gebruikers.Add(gebruiker);
}
}
}
}
return Gebruikers;
;
}
public bool VoegToeGebruiker(string locatie, DateTime DatumTijd)
{
using SqlConnection SqlConn = new SqlConnection("Data Source = (localdb)\\mssqllocaldb; Initial Catalog = Testdatabase; Integrated Security = True");
SqlConn.Open();
using var command = SqlConn.CreateCommand();
command.CommandText = "Insert into Userinfo2 (Locatie,DatumTijd) values (@locatie, @DatumTijd)";
command.Parameters.AddWithValue("@locatie", locatie);
command.Parameters.AddWithValue("@DatumTijd", DatumTijd);
var editLines = command.ExecuteNonQuery();
if (editLines == 1)
{
return true;
}
return false;
}
public bool VerwijderGebruiker()
{
using SqlConnection SqlConn = new SqlConnection("Data Source = (localdb)\\mssqllocaldb; Initial Catalog = Testdatabase; Integrated Security = True");
SqlConn.Open();
using var command = SqlConn.CreateCommand();
command.CommandText = "Delete From Userinfo2";
command.Parameters.Clear();
var verwijderLines = command.ExecuteNonQuery();
if (verwijderLines == 1)
{
return true;
}
return false;
}
}
public class Gebruiker
{
public long ID { get; set; }
public string Locatie { get; set; }
public DateTime DatumTijd { get; set; }
}
}
我的BLL:
namespace BLL
{
public class CLSBussLayer
{
ClsDataLayer objDal = new ClsDataLayer();
public List<Gebruiker> GetGebruikers()
{
return objDal.Testlijst();
}
public bool Addgebruiker(Gebruiker gebruiker)
{
return objDal.VoegToeGebruiker(gebruiker.Locatie, gebruiker.DatumTijd);
//if statements voor verifieren van ingevulde data moeten hier
}
public bool Verwijdergebruiker()
{
return objDal.VerwijderGebruiker();
}
}
我的index. cshtml. cs:
public class IndexModel : PageModel
{
[BindProperty]
public string Locatie { get; set; }
[BindProperty]
public DateTime DatumTijd { get; set; }
private readonly ILogger<IndexModel> _logger;
public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;
bussLayer = new CLSBussLayer();
}
private CLSBussLayer bussLayer{get; set;}
public void OnGet()
{
}
public void OnPost()
{
bussLayer.Addgebruiker(new Gebruiker
{
Locatie = Locatie,
DatumTijd = DatumTijd
}) ;
}
}
我的index.cshtml:
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
BLL.CLSBussLayer bussLayer = new BLL.CLSBussLayer();
var list = bussLayer.GetGebruikers();
}
<link href="~/css/test.css" rel="stylesheet" />
@foreach (Modellayer.Gebruiker gebruiker in list)
{
<div>
@gebruiker.Locatie
@gebruiker.DatumTijd
</div>
}
<div>
<form method="post">
<label for="name">Naam: </label><br />
<input type="text" id="naam" asp-for="Locatie" />
<label for="Datetime">Datum: </label>
<input value="05-19-2020T12:00" min="05-19-2020T12:00" type="datetime-local" id="DateTime" asp-for="DatumTijd" />
<input type="submit" value="Submit" />
<input type=""
</form>
</div>
问题是我不知道如何制作一个按钮(在我的index.html中),其中包含DELETE文本,以执行VerwijderGebruiker方法。我想我已经成功地制作了删除方法,并将其传递给了BLL,但我不知道从哪里开始。有人能帮助我吗?
Hı ,
在你的cshtml中像这样添加按钮。
@foreach (Modellayer.Gebruiker gebruiker in list)
{
<div>
@gebruiker.Locatie
@gebruiker.DatumTijd
</div>
<button onclick="DeleteItem(@gebruiker.ID)">Delete</button>
}
然后,在你的javascript中定义DeleteItem metod和post ajax。(给你的删除动作url)
<script>
function DeleteItem(Id) {
$('#btnDlete').on('click', function () {
var jsonData = JSON.stringify({ str: Id}); //strigify json data
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8", //set content type
url: "YourController/DeleteRecord", //remove the first slash from the url
data: jsonData,
async: false, //wheather to call async or not
success: function (data) {
alert('success'); //success handler
},
error: function (data) {
alert(data); //error handler
}
});
}
</script>