页面显示

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

.NET Core Razor相关推荐

  1. 学习ASP.NET Core Razor 编程系列九——增加查询功能

    原文:学习ASP.NET Core Razor 编程系列九--增加查询功能 学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.N ...

  2. 学习ASP.NET Core Razor 编程系列五——Asp.Net Core Razor新建模板页面

    学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二--添加一个实体 学习ASP.NET ...

  3. ASP.NET Core Razor 标签助手 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core Razor 标签助手 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core Razor 标签助手 上一章节我们介绍了视图导入,学习了 ...

  4. ASP.NET Core - Razor页面之Handlers处理方法

    简介 在前一篇文章中,我们讨论了Razor页面.今天我们来谈谈处理方法(Handlers). 我们知道可以将代码和模型放在 .cshtml 文件里面或与 .cshtml 匹配的 .cshtml.cs ...

  5. 学习ASP.NET Core Razor 编程系列十八——并发解决方案

    原文:学习ASP.NET Core Razor 编程系列十八--并发解决方案 学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP. ...

  6. 学习ASP.NET Core Razor 编程系列十三——文件上传功能(一)

    原文:学习ASP.NET Core Razor 编程系列十三--文件上传功能(一) 学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习A ...

  7. ASP.Net Core Razor 部署AdminLTE框架

    1.AdminLTE 一个基于 bootstrap 的轻量级后台模板 2.AdminLTE 文档 在线中文Demo:http://adminlte.la998.com/ 在线中文文档:http://a ...

  8. ASP.NET Core Razor 页面使用教程

    ASP.NET Core Razor 页面作为 ASP.NET Core 2.0的一部分发布,它是基于页面的全新的Web开发框架.如果您想学习如何使用 ASP.NET Core Razor 页面,可以 ...

  9. ASP.NET Core Razor页面禁用防伪令牌验证

    这篇短文中,我将向您介绍如何ASP.NET Core Razor页面中禁用防伪令牌验证. Razor页面是ASP.NET Core 2.0中增加的一个页面控制器框架,用于构建动态的.数据驱动的网站:支 ...

  10. 如何ASP.NET Core Razor中处理Ajax请求

    在ASP.NET Core Razor(以下简称Razor)刚出来的时候,看了一下官方的文档,一直没怎么用过.今天闲来无事,准备用Rozor做个项目熟练下,结果写第一个页面就卡住了..折腾半天才搞好, ...

最新文章

  1. android 汉字编码,Android解压中文乱码
  2. 项目第一天--ElementUI介绍
  3. asp.net 字符串过滤
  4. 【无码专区11】异或2(结论 / 推式子 + 哈希hash + 大整数高精度 加减乘除重载考察)
  5. java s.charat_Java中s.charAt(index)用于提取字符串s中的特定字符操作
  6. 鼠标中间无法打开新标签_还记得鼠标有几个键?Win10环境鼠标中键的妙用
  7. 不安装oracle 连接数据库,不安装oracle 连接服务器oracle数据库方法
  8. 程序员之常用软件安装过程记录
  9. Linux shell中的那些小把戏
  10. 使用Docker运行oracle11g企业版和简单配置
  11. 【New】简•导航 正式上线
  12. js获取request中的值_基于node.js的开发框架 — Koa
  13. SAP License:系统退货处理流程
  14. Scala 专题指南
  15. 《信号与系统》(吴京)部分课后习题答案与解析——第一章
  16. qwidget设置背景透明_手机上设置“小小科技半透明”主题背景,效果美翻天!...
  17. java读取tif文件_java读取TIF,TIFF文件方法
  18. 深圳市计算机素质测试答案,广东 : 2019年深圳计算机专业素质测试真题
  19. cinta作业6:拉格朗日定理
  20. 什么是MapReduce?MapReduce整体架构搭建使用介绍

热门文章

  1. 双网卡设置静态路由及强制某个运用捆绑指定网卡
  2. 小龙:c++多线程编程三(线程参数)
  3. 下载java免安装包_下载并获取免安装版的JDK、JRE和源码包
  4. mysql 输入汉字全是_mysql中文问题全处理
  5. 强大的抠图换背景工具
  6. python抢票代码_Python自动化xpath实现自动抢票抢货
  7. Sql Server数据库事务介绍(二)---Sql语句,SqlTransaction和TransactionScope的使用方法
  8. 透过管理帧保护看WAPI协议安全性
  9. MVC 模式及其优缺点
  10. Jdk1.8新特性————stream流操作,让代码更优雅