学习ASP.NET Core Blazor编程系列一——综述

通过前面几篇文章的学习,我们的图书修改页面已经能正常运行了,但现在的呈现效果不是很理想,主要标题显示的是英文。我们不想看到的时间(如下图所示0:00:00),并且希望把“ReleaseDate”修改成“出版日期”。现在的程序运行效果如下图。

本文所实现的图书信息修改功能,在前面的路由篇中已经实现了,本文只是将其拿出来,单独讲解一下。

1. 我们在Visual Studio 2022的解决方案资源管理器中打开Pages/BookIndex.razor文件,修改代码如下:

@page "/BookIndex"
@using BlazorAppDemo.Models
@using Microsoft.EntityFrameworkCore  
@inject IDbContextFactory<BookContext> dbFactory
@inject NavigationManager NavigationManager
  <PageTitle>图书列表</PageTitle> <h3>图书列表</h3> <table class="table-responsive" width="90%"> 
<tr><td>书名</td>
<td>作者</td>
<td>价格</td>
<td>出版日期</td>
<td>库存数量</td>
<td>已租数量</td> <td>操作</td>
  </tr> @foreach (var item in books)
{ <tr> 
<td>@item.Name</td>
<td>@item.Author</td>
<td>@item.Price</td>
<td>@item.ReleaseDate.ToShortDateString()</td>
<td>@item.StockQty</td>
<td>@item.Qty</td> <td><a href="/AddBook?Id=@item.ID">编辑</a> <button id="edit" class="btn btn-primary" @onclick="@(e => EditBook(e, @item.ID))">修改</button>
</td> </tr>
}
</table>  
@code { private static BookContext _context; private List<Book> books = new List<Book>();
protected override async Task OnInitializedAsync() {
_context = dbFactory.CreateDbContext();
books=_context.Book.ToList();
await base.OnInitializedAsync(); } public void EditBook(MouseEventArgs e ,int Id)
{ NavigationManager.NavigateTo("/AddBook?Id="+Id.ToString()); } } 

2.在Visual Studio 2022的解决方案资源管理器中,找到 AddBook.razor 组件,使用鼠标左键双击,在文本编辑器中打开。修改代码如下:

@page "/AddBook"
@using BlazorAppDemo.Models @using Microsoft.EntityFrameworkCore
@inject IDbContextFactory<BookContext> dbFactory
@inject NavigationManager NavigationManager  
<h3>AddBook</h3> <EditForm Model=@addBook OnValidSubmit=@ValidSubmitInfo OnInvalidSubmit=@InvalidSubmitInfo>
<DataAnnotationsValidator />
<ValidationSummary />  
<div>@Message</div> <p> 图书名称:
<InputText @bind-Value=addBook.Name></InputText> <ValidationMessage For="@(() => addBook.Name)" />
</p>
<p>作者:
<InputText @bind-Value=addBook.Author></InputText>
<ValidationMessage For="@(() => addBook.Author)" />
</p>
    
<p>出版日期:
<InputDate @bind-Value=addBook.ReleaseDate></InputDate> </p>
<p>价格:
<InputNumber @bind-Value=addBook.Price></InputNumber> </p>
<p>类型:
<InputText @bind-Value=addBook.Type></InputText>
<ValidationMessage For="@(() => addBook.Type)" />
</p> <p>总页数:
<InputNumber @bind-Value=addBook.TotalPages></InputNumber>
</p>
<p>库存数量:
<InputNumber @bind-Value=addBook.StockQty></InputNumber> <ValidationMessage For="@(() => addBook.StockQty)" /> </p>
<p>已租数量:
<InputNumber @bind-Value=addBook.Qty></InputNumber>
</p>
<input type="submit" class="btn btn-primary" value="Save" /> </EditForm> @code {
private string Message = string.Empty;
private static BookContext _context; private Models.Book addBook = new Book();
protected override Task OnInitializedAsync()
{
_context = dbFactory.CreateDbContext();
return base.OnInitializedAsync();
}
 
public string Id { get; set; }
 
protected override void OnParametersSet()
{ var query = new Uri(NavigationManager.Uri).Query; var queryDic = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(query);  
if (queryDic.Count > 0)
{
Id = queryDic["Id"].ToString() ?? "0";
if (!string.IsNullOrEmpty(Id))
{
if (Id != "0")
{
int iId = int.Parse(Id);
var book = _context.Book.Find(iId);
if (book != null)
{
addBook = book;
}
}
 
}
}
}
 
 
private void ValidSubmitInfo(EditContext editContext)
{
 
if (editContext.Model is not Book addBook)
{
Message = "你编辑的不是图书信息,校验通过。正在保存。";
 
}
Save(editContext);
}
private void InvalidSubmitInfo(EditContext editContext)
{
if (editContext.Model is not Book addBook)
 
{
Message = "你编辑的图书信息校验不通过,请修改。";
}
 
}
 
private void Save(EditContext editContext)
{
bool dataIsValid = editContext.Validate(); if (!dataIsValid)
{
Message = "你编辑的图书信息校验不通过,请修改。";
return;
}
  if (editContext.Model is not Book addBook)
{
Message = "你编辑的不是图书信息。";
return;
}
if (string.IsNullOrEmpty(Id) || Id == "0" )
{
_context.Add(editContext.Model);
} int cnt= _context.SaveChanges(); if (cnt>0)
{
Message = "图书信息保存成功!"; }else
{
Message = "图书信息保存失败!";
 
}
      
}
}

3. 在Visual Studio 2022的菜单栏上,找到“调试à开始调试”或是按F5键,Visual Studio 2022会生成BlazorAppDemo应用程序,并在浏览器中打开Home页面,我们我们使用鼠标左键点击左边菜单上的“图书列表”菜单项,浏览器会显示我们修改过的图书列表页面。如下图。

4.在浏览器的图书列表页面,将鼠标指针悬停在“编辑”按钮上以查看,链接背后的URL值。使用鼠标左键点击我们要修改的图书信息的“编辑”按钮,浏览器会自动跳转到AddBook页面,并将我们需要修改的图书信息显示出来。如下图。

5.在AddBook页面中的“类型”与“已租数量”中分别填入“A”与“2”,然后使用鼠标点击“保存”按钮。如下图。

学习ASP.NET Core Blazor编程系列十四——修改的更多相关文章

  1. 学习ASP.NET Core Blazor编程系列十——路由(中)

    学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应用程序(上) 学习ASP.NET Core Blazor编程系 ...

  2. 学习ASP.NET Core Blazor编程系列十——路由(上)

    学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应用程序(上) 学习ASP.NET Core Blazor编程系 ...

  3. 学习ASP.NET Core Razor 编程系列十四——文件上传功能(二)

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

  4. 学习ASP.NET Core Razor 编程系列十九——分页

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

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

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

  6. 学习ASP.NET Core Razor 编程系列十六——排序

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

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

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

  8. 学习ASP.NET Core Blazor编程系列八——数据校验

    学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应用程序(上) 学习ASP.NET Core Blazor编程系 ...

  9. 学习ASP.NET Core Blazor编程系列九——服务器端校验

    学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应用程序(上) 学习ASP.NET Core Blazor编程系 ...

  10. 学习ASP.NET Core Razor 编程系列十二——在页面中增加校验

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

随机推荐

  1. Go编译过程

    一. Go编译流程 二.过程说明 1. 词法解析 读取Go源文件,将字符序列转换为符号(token)序列,比如将":="转换为_Define 代码中的标识符.关键字.运算符和分隔符 ...

  2. LFS(Linux From Scratch)构建过程全记录(五):交叉工具链的构建

    写在前面 本文将详细讲述如何构建工具链 前置知识 在LFS-BOOK中,我们需要学习一些关于"交叉编译"的内容,详见书本 安装Binutils-2.39 我们cd到sources文 ...

  3. BUUCTF Misc 被偷走的文件

    首先下载文件打开 得到一个流量文件 用wireshark打开 打开后 进行分析 看到有ftp流量,于是过滤ftp 看到被偷走的是flag.rar 接下用binwalk进行分离 binwalk -e f ...

  4. Grafana Loki 架构

    转载自:https://mp.weixin.qq.com/s?__biz=MzU4MjQ0MTU4Ng==&mid=2247492186&idx=2&sn=a06954384a ...

  5. Nginx+lua+openresty精简系列

    1. CentOS系统安装openresty 你可以在你的 CentOS 系统中添加 openresty 仓库,这样就可以便于未来安装或更新我们的软件包(通过 yum update 命令).运行下面的 ...

  6. 官方使用logstash同步Mysql数据表到ES的摘抄

    官方文档地址:https://www.elastic.co/guide/en/logstash/current/plugins-inputs-jdbc.html#plugins-inputs-jdbc ...

  7. 17. Fluentd输出插件:out_copy用法详解

    copy即复制,out_copy的作用就是将日志事件复制到多个输出,这样就可以对同一份日志做不同类型的分析处理. out_copy内置于Fluentd,无需单独安装. 示例配置 <match p ...

  8. 记录Gerrit2.8.4环境迁移、安装、配置以及问题解决

    转载自:https://cloud.tencent.com/developer/article/1010629 说到gerrit,没听说的同学可能会感到比较陌生,那么先来copy一段关于gerrit的 ...

  9. 多云容器编排 Karmada-Operator 实践

    作者:vivo 互联网服务器团队-Zhang Rong Karmada作为开源的云原生多云容器编排项目,吸引了众多企业共同参与项目开发,并运行于生产环境中.同时多云也逐步成为数据中心建设的基础架构,多 ...

  10. Springboot集成阿里云短信

    目录 1 前言 2 准备工作 2.1 了解流程 2.2 配置信息 2.3 短信签名和模板 2.3.1 签名 2.3.2 模板 2.3.3 存入数据库 3 SDK 4 集成Springboot 4.1 ...