我想在表中添加一个“存档”按钮,以便在单击该行中的数据时将其移动到另一个名为“存档”的视图中,我已编辑并删除了所有已找出的内容。与软删除不同,该记录将从到存档视图的索引。这可能很简单,但我不知道如何。 我不知道是否需要用于存档的新模型或表中已存档的新列,但是正如我提到的,“存档”按钮应仅将记录移至包含所有存档记录的其他视图,也许我需要在数据库中为主表中的所有已归档数据创建一个新表..这是我到目前为止的内容: 查看:
@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");
}
谢谢。...
答案 0 :(得分:1)
在我看来,您可以按以下两种方式进行操作,具体取决于您以后要如何处理数据。根据您的建议创建一个新的存档表,或者只是在当前的客户表中添加一个额外的列。
如果创建新表,则首先将所需的值从“客户”表复制到“存档”表,然后从“客户”表中删除数据。
或者,您可以在“客户”表中添加一个额外的列,例如IsArchived,并使其成为布尔值(SqL中的位)。将“已归档客户”设置为true,否则设置为false。 然后,您可以根据此值在不同的Controller中过滤客户。
如果表中已经有数据,请将其设置为可为null的布尔值(bool?)-(SqL-允许为空),并将null值视为false。
修改
如果人们有相同的问题:
在模型中添加:
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