.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);}}
}
.NET Core Razor相关推荐
- 学习ASP.NET Core Razor 编程系列九——增加查询功能
原文:学习ASP.NET Core Razor 编程系列九--增加查询功能 学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.N ...
- 学习ASP.NET Core Razor 编程系列五——Asp.Net Core Razor新建模板页面
学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二--添加一个实体 学习ASP.NET ...
- ASP.NET Core Razor 标签助手 - ASP.NET Core 基础教程 - 简单教程,简单编程
原文:ASP.NET Core Razor 标签助手 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core Razor 标签助手 上一章节我们介绍了视图导入,学习了 ...
- ASP.NET Core - Razor页面之Handlers处理方法
简介 在前一篇文章中,我们讨论了Razor页面.今天我们来谈谈处理方法(Handlers). 我们知道可以将代码和模型放在 .cshtml 文件里面或与 .cshtml 匹配的 .cshtml.cs ...
- 学习ASP.NET Core Razor 编程系列十八——并发解决方案
原文:学习ASP.NET Core Razor 编程系列十八--并发解决方案 学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP. ...
- 学习ASP.NET Core Razor 编程系列十三——文件上传功能(一)
原文:学习ASP.NET Core Razor 编程系列十三--文件上传功能(一) 学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习A ...
- ASP.Net Core Razor 部署AdminLTE框架
1.AdminLTE 一个基于 bootstrap 的轻量级后台模板 2.AdminLTE 文档 在线中文Demo:http://adminlte.la998.com/ 在线中文文档:http://a ...
- ASP.NET Core Razor 页面使用教程
ASP.NET Core Razor 页面作为 ASP.NET Core 2.0的一部分发布,它是基于页面的全新的Web开发框架.如果您想学习如何使用 ASP.NET Core Razor 页面,可以 ...
- ASP.NET Core Razor页面禁用防伪令牌验证
这篇短文中,我将向您介绍如何ASP.NET Core Razor页面中禁用防伪令牌验证. Razor页面是ASP.NET Core 2.0中增加的一个页面控制器框架,用于构建动态的.数据驱动的网站:支 ...
- 如何ASP.NET Core Razor中处理Ajax请求
在ASP.NET Core Razor(以下简称Razor)刚出来的时候,看了一下官方的文档,一直没怎么用过.今天闲来无事,准备用Rozor做个项目熟练下,结果写第一个页面就卡住了..折腾半天才搞好, ...
最新文章
- android 汉字编码,Android解压中文乱码
- 项目第一天--ElementUI介绍
- asp.net 字符串过滤
- 【无码专区11】异或2(结论 / 推式子 + 哈希hash + 大整数高精度 加减乘除重载考察)
- java s.charat_Java中s.charAt(index)用于提取字符串s中的特定字符操作
- 鼠标中间无法打开新标签_还记得鼠标有几个键?Win10环境鼠标中键的妙用
- 不安装oracle 连接数据库,不安装oracle 连接服务器oracle数据库方法
- 程序员之常用软件安装过程记录
- Linux shell中的那些小把戏
- 使用Docker运行oracle11g企业版和简单配置
- 【New】简•导航 正式上线
- js获取request中的值_基于node.js的开发框架 — Koa
- SAP License:系统退货处理流程
- Scala 专题指南
- 《信号与系统》(吴京)部分课后习题答案与解析——第一章
- qwidget设置背景透明_手机上设置“小小科技半透明”主题背景,效果美翻天!...
- java读取tif文件_java读取TIF,TIFF文件方法
- 深圳市计算机素质测试答案,广东 : 2019年深圳计算机专业素质测试真题
- cinta作业6:拉格朗日定理
- 什么是MapReduce?MapReduce整体架构搭建使用介绍
热门文章
- 双网卡设置静态路由及强制某个运用捆绑指定网卡
- 小龙:c++多线程编程三(线程参数)
- 下载java免安装包_下载并获取免安装版的JDK、JRE和源码包
- mysql 输入汉字全是_mysql中文问题全处理
- 强大的抠图换背景工具
- python抢票代码_Python自动化xpath实现自动抢票抢货
- Sql Server数据库事务介绍(二)---Sql语句,SqlTransaction和TransactionScope的使用方法
- 透过管理帧保护看WAPI协议安全性
- MVC 模式及其优缺点
- Jdk1.8新特性————stream流操作,让代码更优雅