将记录移动到存档视图 非软删除
Move record to Archived View Not soft delete
我想在 table 中添加一个存档按钮,这样当它被点击时,该行中的数据就会移动到另一个名为存档的视图中,我已经弄清楚了编辑和删除所有内容。与软删除不同记录只会从索引移动到存档视图。这可能很简单,但我不知道如何。
我不知道我是否需要一个新的存档模型或我的 table 中的新列表示已存档,但正如我提到的,存档按钮应该只将记录移动到具有所有存档记录的不同视图,也许我需要在数据库中为来自主 table 的所有存档数据创建一个新的 table。这是我目前所拥有的:
查看:
@model IEnumerable<practice2.Models.Customer>
@{
ViewBag.Title = "Archive";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Archive</h2>
<div class="container">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th> User Name</th>
<th>Date Of Birth</th>
<th>Membership Type</th>
<th>Email</th>
</tr>
</thead>
<tbody>
@foreach (var customer in Model)
{
<tr>
<td>@Html.ActionLink(customer.LastName, "Details", "Customers", new { id = customer.Id }, null)</td>
<td>@customer.DateOfBirth.Value.ToShortDateString()</td>
<td>@customer.MembershipType.NameOfMembershipType</td>
<td>@customer.EmailAddress</td>
</tr>
}
</tbody>
</table>
</div>
控制器:
public ActionResult Archive (int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Customer customer = _context.Customer.Find(id);
if (customer == null)
{
return HttpNotFound();
}
_context.Customer.Move(customer)to archive;// I wish it was this easy
_context.SaveChanges();
return RedirectToAction("Index", "Customers");
}
谢谢....
在我看来,您可以采用两种方式,具体取决于您之后想要如何处理数据。按照您的建议创建一个新存档 table 或简单地向您当前的客户 table.
添加一个额外的列
如果您创建一个新的 table,您首先将需要的值从您的客户 table 复制到您的存档 table,然后从客户 table 中删除数据.
或者,您向客户添加一个额外的列 table 例如IsArchived,并将其设为布尔值(SqL 中的位)。 "Customer is archived" 设置为 true,否则设置为 false。
然后你可以根据这个值在不同的Controller中过滤你的客户。
如果 table 中已有数据,请将其设为可为空的布尔值(bool?)-(SqL - 允许为空)并将 null-values 视为 false。
编辑
In Case People have same question:
输入模型地址:
public bool? IsArchived { set; get; }
Nu-get
Add-migration AddNewColumnToWhatever
Update-database
在控制器中:
public ActionResult Archive(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Customer customer = _context.Customer.Find(id);
if (customer == null)
{
return HttpNotFound();
}
customer.IsArchived = true;
return RedirectToAction("Index", "Customers");
}
public ActionResult GetCustomers(string typeOfCustomer)
{
List<Customer> customers = new List<Customer>();
if (string.IsNullOrEmpty(typeOfCustomer))
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
if (typeOfCustomer == "archived")
{
customers = _context.Customer.Include(c => c.MembershipType).Where(x => x.IsArchived == true).Select(x => x).ToList();
}
else if (typeOfCustomer == "active")
{
customers = _context.Customer.Include(c => c.MembershipType).Where(x => x.IsArchived == false).Select(x => x).ToList();
}
else
{
return HttpNotFound();
}
return View(customers);
}
为 GetCustomers 创建一个新视图并在其中定义模型:
@model List<YourProject.FolderWhereYourClassIsDefined.Customer>
并根据需要构建视图。
在您现有的视图中,放置 2 个按钮以调用新的 ActionResult。
@Html.ActionLink("View Archived", "GetCustomers", "Customers", new { typeOfCustomer = "archived" }, new { @class = "btn" })
@Html.ActionLink("View Active", "GetCustomers", "Customers", new { typeOfCustomer = "active" }, new { @class = "btn" })//You don't need it
现在在索引控制器中执行此操作:
var customer = _context.Customer.Include(c => c.MembershipType)
.Where(c => c.IsArchived == null)// important
.Where(c => c.IsArchived == false) // important
.Include(c => c.CardType)
.ToList();//to excute query immediatly
你应该一切都好。 LasseHolm/EndlessQ
我想在 table 中添加一个存档按钮,这样当它被点击时,该行中的数据就会移动到另一个名为存档的视图中,我已经弄清楚了编辑和删除所有内容。与软删除不同记录只会从索引移动到存档视图。这可能很简单,但我不知道如何。 我不知道我是否需要一个新的存档模型或我的 table 中的新列表示已存档,但正如我提到的,存档按钮应该只将记录移动到具有所有存档记录的不同视图,也许我需要在数据库中为来自主 table 的所有存档数据创建一个新的 table。这是我目前所拥有的: 查看:
@model IEnumerable<practice2.Models.Customer>
@{
ViewBag.Title = "Archive";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Archive</h2>
<div class="container">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th> User Name</th>
<th>Date Of Birth</th>
<th>Membership Type</th>
<th>Email</th>
</tr>
</thead>
<tbody>
@foreach (var customer in Model)
{
<tr>
<td>@Html.ActionLink(customer.LastName, "Details", "Customers", new { id = customer.Id }, null)</td>
<td>@customer.DateOfBirth.Value.ToShortDateString()</td>
<td>@customer.MembershipType.NameOfMembershipType</td>
<td>@customer.EmailAddress</td>
</tr>
}
</tbody>
</table>
</div>
控制器:
public ActionResult Archive (int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Customer customer = _context.Customer.Find(id);
if (customer == null)
{
return HttpNotFound();
}
_context.Customer.Move(customer)to archive;// I wish it was this easy
_context.SaveChanges();
return RedirectToAction("Index", "Customers");
}
谢谢....
在我看来,您可以采用两种方式,具体取决于您之后想要如何处理数据。按照您的建议创建一个新存档 table 或简单地向您当前的客户 table.
添加一个额外的列如果您创建一个新的 table,您首先将需要的值从您的客户 table 复制到您的存档 table,然后从客户 table 中删除数据.
或者,您向客户添加一个额外的列 table 例如IsArchived,并将其设为布尔值(SqL 中的位)。 "Customer is archived" 设置为 true,否则设置为 false。 然后你可以根据这个值在不同的Controller中过滤你的客户。
如果 table 中已有数据,请将其设为可为空的布尔值(bool?)-(SqL - 允许为空)并将 null-values 视为 false。
编辑
In Case People have same question:
输入模型地址:
public bool? IsArchived { set; get; }
Nu-get
Add-migration AddNewColumnToWhatever
Update-database
在控制器中:
public ActionResult Archive(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Customer customer = _context.Customer.Find(id);
if (customer == null)
{
return HttpNotFound();
}
customer.IsArchived = true;
return RedirectToAction("Index", "Customers");
}
public ActionResult GetCustomers(string typeOfCustomer)
{
List<Customer> customers = new List<Customer>();
if (string.IsNullOrEmpty(typeOfCustomer))
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
if (typeOfCustomer == "archived")
{
customers = _context.Customer.Include(c => c.MembershipType).Where(x => x.IsArchived == true).Select(x => x).ToList();
}
else if (typeOfCustomer == "active")
{
customers = _context.Customer.Include(c => c.MembershipType).Where(x => x.IsArchived == false).Select(x => x).ToList();
}
else
{
return HttpNotFound();
}
return View(customers);
}
为 GetCustomers 创建一个新视图并在其中定义模型:
@model List<YourProject.FolderWhereYourClassIsDefined.Customer>
并根据需要构建视图。
在您现有的视图中,放置 2 个按钮以调用新的 ActionResult。
@Html.ActionLink("View Archived", "GetCustomers", "Customers", new { typeOfCustomer = "archived" }, new { @class = "btn" })
@Html.ActionLink("View Active", "GetCustomers", "Customers", new { typeOfCustomer = "active" }, new { @class = "btn" })//You don't need it
现在在索引控制器中执行此操作:
var customer = _context.Customer.Include(c => c.MembershipType)
.Where(c => c.IsArchived == null)// important
.Where(c => c.IsArchived == false) // important
.Include(c => c.CardType)
.ToList();//to excute query immediatly
你应该一切都好。 LasseHolm/EndlessQ