我在控制器和我的dal中发布删除操作的代码。我昨天一直在谷歌上搜索它,但不能设法在我的实施其他想法。
P.S。我使用bool的原因是我看到了一个使用它的教程,也许我应该使用更适合我的项目的其他东西?
DAL
public bool DeleteMembers()
{
con.Open();
SqlCommand cmd =
new SqlCommand("delete from member where SocialSecurity=@SocialSecurity",
con);
cmd.ExecuteNonQuery();
con.Close();
return true;
}
TController
public bool DeleteMembers()
{
bool cmd = dal.DeleteMembers();
return cmd;
}
事件处理程序
private void btnDelete_Click(object sender, EventArgs e)
{
try...
}
我还设法做了一个foreach方法,删除了我的datagridview中的行。
foreach (DataGridViewRow item in this.dtGrid1.SelectedRows)
{
dtGrid1.Rows.RemoveAt(item.Index);
}
P.S。我不使用参数。所以没有带参数的代码,我没有受过足够的教育,我们不需要在我们的项目中。我知道sql注入。
答案 0 :(得分:0)
GridView组件不适用于ASP.NET MVC应用程序。该组件在很大程度上依赖于ViewState和PostBack,它们是ASP.NET MVC中不再存在的概念。
您可以使用WebGrid helper
在视图中显示记录并提供删除功能。
所以第一步是设计一个模型来表示你想要在视图上显示的记录:
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string SocialSecurity { get; set; }
}
然后下一步是建立一个数据层,用于查询关系数据库并提取模型。您可以使用ORM(如Entity Framework)来简化此任务或直接使用普通的ADO.NET,这将在此处说明:
public class PeopleRepository
{
public IList<Person> Get()
{
var connectionString = ConfigurationManager
.ConnectionString["SomeConnectionStringKey"]
.ConnectionString;
using (var conn = new SqlConnection(connectionString))
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = "SELECT id, fname, lname, ssn FROM people";
using (var reader = cmd.ExecuteReader())
{
var result = new List<Person>();
while (reader.Read())
{
result.Add(new Person
{
Id = reader.GetInt32(reader.GetOrindal("id")),
FirstName = reader.GetString(reader.GetOrindal("fname")),
LastName = reader.GetString(reader.GetOrindal("lname")),
SocialSecurity = reader.GetString(reader.GetOrindal("ssn"))
});
}
return result;
}
}
}
public void Delete(int id)
{
var connectionString = ConfigurationManager
.ConnectionString["SomeConnectionStringKey"]
.ConnectionString;
using (var conn = new SqlConnection(connectionString))
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = "DELETE FROM people WHERE id = @id";
cmd.Parameters.AddWithValue("@id", id);
cmd.ExecuteNonQuery();
}
}
}
下一步是让控制器在模型和视图之间进行连线:
public class PeopleController: Controller
{
private readonly PeopleRepository repo = new PeopleRepository();
public ActionResult Index()
{
IList<Person> model = repo.Get();
return View(model);
}
[HttpPost]
public ActionResult Delete(int id)
{
repo.Delete(id);
IList<Person> model = repo.Get();
return View("Index", model);
}
}
当然最后一步是观点:
@model IList<Person>
@{
var grid = new WebGrid(Model);
}
@grid.GetHtml(
columns: grid.Columns(
grid.Column(columnName: "Id"),
grid.Column(columnName: "FirstName", header: "First name"),
grid.Column(columnName: "LastName", header: "Last name"),
grid.Column(columnName: "SocialSecurity", header: "Social Security Number"),
grid.Column(
header: "",
format:
@<text>
<form action="@Url.Action("Delete", "People", new { id = item.Id })" method="post" onsubmit="return confirm('Are you sure you want to delete this person?');">
<button type="submit">Delete</button>
</form>
</text>
)
)
)