页面显示
@page
@Html.AntiForgeryToken()
@model Webcore.Pages.ListModel
@{
ViewData["Title"] = "List";
}
<h1>List</h1>
<form method="post">
<p>
物品名称: <input type="text" name="PName" style="height:35px;padding-bottom: 6px;" />
<select name="CatId" asp-items="Model.Category" style="width:130px;height:35px;padding-bottom:5px">
<option value="0">请选择</option>
</select>
<input class="btn btn-success" type="submit" value="查 询" />
<a class="btn btn-info" asp-page="Add">添 加</a>
</p>
</form>
@using (Html.BeginForm(FormMethod.Post))
{
<div>
<p>
物品名称: <input type="text" asp-for="PName" style="height:35px;padding-bottom: 6px;" />
<select asp-for="CatId" asp-items="Model.Category" style="width:130px;height:35px;padding-bottom:5px">
<option value="0">请选择</option>
</select>
<input class="btn btn-success" type="submit" value="查 询" />
<a class="btn btn-info" asp-page="Add">添 加</a>
</p>
</div>
}
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Product[0].ProductName)
</th>
<th>
@Html.DisplayNameFor(model => model.Product[0].IsUp)
</th>
<th>
@Html.DisplayNameFor(model => model.Product[0].UnitPrice)
</th>
<th>
@Html.DisplayNameFor(model => model.Product[0].Remark)
</th>
<th>
@Html.DisplayNameFor(model => model.Product[0].Category)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Product)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.ProductName)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsUp)
</td>
<td>
@Html.DisplayFor(modelItem => item.UnitPrice)
</td>
<td>
@Html.DisplayFor(modelItem => item.Remark)
</td>
<td>
@Html.DisplayFor(modelItem => item.Category.CategoryName)
</td>
<td>
<a asp-page="./Edit" asp-route-id="@item.ProductId">编辑</a> |
<a href="javascript:void(0);" data-del="@item.ProductId">删除</a>
</td>
</tr>
}
</tbody>
</table>
@section scripts{
<script type="text/javascript">
$("a[data-del]").click(function () {
var $this = $(this);
var id = $this.attr("data-del");
if (confirm("确定要删除吗?")) {
$.ajax({
type: "POST",
contentType: "application/x-www-form-urlencoded",
url: "?handler=Delete",
data: { id: id },
success: function (data) {
if (data == "yes") {
alert("删除成功");
$this.closest("tr").remove();
} else {
alert("删除失败");
}
},
error: function () {
alert("程序异常!查询出错"); return;
}
}).then(function (row) {
console.log(row);
})
}
});
</script>
}
?页面显示和查询
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Webcore.Model;
using Webcore.Model.Entity;
namespace Webcore.Pages
{
public class ListModel : PageModel
{
private readonly Webcore.Model.EntityDbContext _context;
public ListModel(Webcore.Model.EntityDbContext context)
{
_context = context;
}
public IList<Product> Product { get; set; }
public SelectList Category;
public int CatId { get; set; }
public string PName { get; set; }
public async Task OnGetAsync(int CatId, string PName)
{
var product = from m in _context.Products.Include(p => p.Category) select m;
if (!string.IsNullOrEmpty(PName))
{
product = product.Where(s => s.ProductName.Contains(PName));
}
if (CatId != 0)
{
product = product.Where(s => s.CategoryId == CatId);
}
Category = new SelectList(_context.Categories, "CategoryId", "CategoryName");
Product = await product.ToListAsync();
}
public async Task OnPostAsync(int CatId, string PName)
{
var product = from m in _context.Products.Include(p => p.Category) select m;
if (!string.IsNullOrEmpty(PName))
{
product = product.Where(s => s.ProductName.Contains(PName));
}
if (CatId != 0)
{
product = product.Where(s => s.CategoryId == CatId);
}
Category = new SelectList(_context.Categories, "CategoryId", "CategoryName");
Product = await product.ToListAsync();
}
public async Task<IActionResult> OnPostDelete(int id)
{
if (!ModelState.IsValid)
{
return Page();
}
var model = _context.Products.FirstOrDefault(b => b.ProductId == id);
if (model == null)
{
return new JsonResult("no");
}
_context.Products.Remove(model);
await _context.SaveChangesAsync();
return new JsonResult("yes");
}
}
}
?添加
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.EntityFrameworkCore; using Webcore.Model; using Webcore.Model.Entity;
namespace Webcore.Pages { ? ? public class ListModel : PageModel ? ? { ? ? ? ? private readonly Webcore.Model.EntityDbContext _context;
? ? ? ? public ListModel(Webcore.Model.EntityDbContext context) ? ? ? ? { ? ? ? ? ? ? _context = context; ? ? ? ? } ? ? ? ? public IList<Product> Product { get; set; }
? ? ? ? public SelectList Category; ? ? ? ? public int CatId { get; set; }
? ? ? ? public string PName { get; set; } ? ? ? ? public async Task OnGetAsync(int CatId, string PName) ? ? ? ? { ? ? ? ? ? ? var product = from m in _context.Products.Include(p => p.Category) select m;
? ? ? ? ? ? if (!string.IsNullOrEmpty(PName)) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? product = product.Where(s => s.ProductName.Contains(PName)); ? ? ? ? ? ? } ? ? ? ? ? ? if (CatId != 0) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? product = product.Where(s => s.CategoryId == CatId); ? ? ? ? ? ? }
? ? ? ? ? ? Category = new SelectList(_context.Categories, "CategoryId", "CategoryName"); ? ? ? ? ? ? Product = await product.ToListAsync(); ? ? ? ? }
? ? ? ? public async Task OnPostAsync(int CatId, string PName) ? ? ? ? { ? ? ? ? ? ? var product = from m in _context.Products.Include(p => p.Category) select m;
? ? ? ? ? ? if (!string.IsNullOrEmpty(PName)) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? product = product.Where(s => s.ProductName.Contains(PName)); ? ? ? ? ? ? } ? ? ? ? ? ? if (CatId != 0) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? product = product.Where(s => s.CategoryId == CatId); ? ? ? ? ? ? }
? ? ? ? ? ? Category = new SelectList(_context.Categories, "CategoryId", "CategoryName"); ? ? ? ? ? ? Product = await product.ToListAsync(); ? ? ? ? }
? ? ? ? public async Task<IActionResult> OnPostDelete(int id) ? ? ? ? { ? ? ? ? ? ? if (!ModelState.IsValid) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? return Page(); ? ? ? ? ? ? } ? ? ? ? ? ? var model = _context.Products.FirstOrDefault(b => b.ProductId == id); ? ? ? ? ? ? if (model == null) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? return new JsonResult("no"); ? ? ? ? ? ? } ? ? ? ? ? ? _context.Products.Remove(model); ? ? ? ? ? ? await _context.SaveChangesAsync(); ? ? ? ? ? ? return new JsonResult("yes"); ? ? ? ? }
? ? } } ?
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.Rendering; using Webcore.Model; using Webcore.Model.Entity;
namespace Webcore.Pages { ? ? public class AddModel : PageModel ? ? { ? ? ? ? private readonly Webcore.Model.EntityDbContext _context;
? ? ? ? public AddModel(Webcore.Model.EntityDbContext context) ? ? ? ? { ? ? ? ? ? ? _context = context; ? ? ? ? }
? ? ? ? public IActionResult OnGet() ? ? ? ? { ? ? ? ? ? ? ViewData["CategoryId"] = new SelectList(_context.Categories, "CategoryId", "CategoryName"); ? ? ? ? ? ? return Page(); ? ? ? ? }
? ? ? ? [BindProperty] ? ? ? ? public Product Product { get; set; }
? ? ? ? public async Task<IActionResult> OnPostAsync() ? ? ? ? { ? ? ? ? ? ? if (!ModelState.IsValid) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? return Page(); ? ? ? ? ? ? } ? ? ? ? ? ? _context.Products.Add(Product); ? ? ? ? ? ? await _context.SaveChangesAsync();
? ? ? ? ? ? return RedirectToPage("./List"); ? ? ? ? }
? ? } } ?
修改?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Webcore.Model;
using Webcore.Model.Entity;
namespace Webcore.Pages
{
public class EditModel : PageModel
{
private readonly Webcore.Model.EntityDbContext _context;
public EditModel(Webcore.Model.EntityDbContext context)
{
_context = context;
}
[BindProperty]
public Product Product { get; set; }
public async Task<IActionResult> OnGetAsync(int? id)
{
if (id == null)
{
return NotFound();
}
Product = await _context.Products
.Include(p => p.Category).FirstOrDefaultAsync(m => m.ProductId == id);
if (Product == null)
{
return NotFound();
}
ViewData["CategoryId"] = new SelectList(_context.Categories, "CategoryId", "CategoryName");
return Page();
}
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
_context.Attach(Product).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ProductExists(Product.ProductId))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToPage("./List");
}
private bool ProductExists(int id)
{
return _context.Products.Any(e => e.ProductId == id);
}
}
}
|