IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> .NET Core Razor页面 增删改查 -> 正文阅读

[开发测试].NET Core Razor页面 增删改查

页面显示

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

  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章      下一篇文章      查看所有文章
加:2021-08-25 12:30:46  更:2021-08-25 12:31:15 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/10 19:57:09-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码