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

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在ASP.NET Core Razor编程中,列表模板页面是非常常见的。这些页面通常用于显示数据库或其他数据源中的一组记录。 在本文中,我将向您展示如何使用ASP.NET Core Razor列表模板页面。 首先,我们需要创建一个模型类来代表我们的数据。例如,假设我们正在构建一个博客应用程序,我们需要一个名为“Post”的模型类来表示博客文章。以下是一个示例模型类: ```csharp public class Post { public int Id { get; set; } public string Title { get; set; } public string Content { get; set; } public DateTime CreatedDate { get; set; } } ``` 接下来,我们需要创建一个控制器类来处理与“Post”模型类相关的操作。以下是一个示例控制器类: ```csharp public class PostController : Controller { private readonly ApplicationDbContext _context; public PostController(ApplicationDbContext context) { _context = context; } public IActionResult Index() { var posts = _context.Posts.ToList(); return View(posts); } } ``` 在此示例控制器中,我们从数据库中检索所有博客文章,并将它们传递给视图。 现在,我们需要创建一个视图来显示我们的博客文章列表。我们可以使用ASP.NET Core Razor模板引擎来创建一个动态模板,该模板可以将我们的博客文章显示为HTML表格。以下是一个示例视图: ```html @model IEnumerable<Post> <table> <thead> <tr> <th>Title</th> <th>Content</th> <th>Created Date</th> </tr> </thead> <tbody> @foreach (var post in Model) { <tr> <td>@post.Title</td> <td>@post.Content</td> <td>@post.CreatedDate.ToShortDateString()</td> </tr> } </tbody> </table> ``` 在此示例视图中,我们使用了一个foreach循环遍历我们的博客文章,并将它们显示为HTML表格行。 最后,我们需要在控制器的Index方法中返回视图。在我们的示例控制器中,我们已经传递了一个包含所有博客文章的IEnumerable<Post>对象。我们可以将此对象传递给视图,如下所示: ```csharp public IActionResult Index() { var posts = _context.Posts.ToList(); return View(posts); } ``` 现在,当我们访问PostController的Index操作时,我们将看到一个包含所有博客文章的HTML表格。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值