.NET Core Razor

这是一个关于使用ASP.NET Core MVC进行页面展示和数据查询的示例,包括物品列表的分页查询、筛选以及通过AJAX实现的删除功能。页面中包含物品名称输入框、类别选择下拉框和查询按钮,同时提供了添加和编辑链接。在后台,`OnGetAsync`和`OnPostAsync`方法处理查询逻辑,`OnPostDelete`方法处理删除操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

页面显示

@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);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值