Blazor实战——Known框架增删改查导

2023-05-25 09:30:07 来源:博客园


(资料图片)

本章介绍学习增、删、改、查、导功能如何实现,下面以商品资料作为示例,该业务栏位如下:

类型、编码、名称、规格、单位、库存下限、库存上限、备注

1. 前后端共用1.1. 创建实体类在KIMS项目Entities文件夹下创建KmGoods实体类该类继承EntityBase类属性使用Column特性描述,用于生成页面字段和数据校验
public class KmGoods : EntityBase{    [Column("商品类型", "", true, "1", "50")]    public string? Type { get; set; }......    [Column("库存下限", "", false)]    public decimal? MinStock { get; set; }......    [Column("备注", "", false)]    public string? Note { get; set; }}
1.2. 创建Client类在KIMS项目Clients文件夹下创建GoodsClient类该类是前后端数据交互接口,继承BaseClient类该类只需提供分页查询、删除和保存,导入功能由框架统一异步处理
public class GoodsClient : BaseClient{    public GoodsClient(Context context) : base(context) { }    public Task> QueryGoodsesAsync(PagingCriteria criteria) => Context.QueryAsync("Goods/QueryGoodses", criteria);    public Task DeleteGoodsesAsync(List models) => Context.PostAsync("Goods/DeleteGoodses", models);    public Task SaveGoodsAsync(object model) => Context.PostAsync("Goods/SaveGoods", model);}
2. 前端2.1. 创建List页面在KIMS.Razor项目BaseData文件夹下创建GoodsList类该类是数据列表页面,继承WebGridView类列表页面按钮和栏位在框架模块管理中配置
class GoodsList : WebGridView{    //分页查询    protected override Task> OnQueryData(PagingCriteria criteria)    {        return Client.Goods.QueryGoodsesAsync(criteria);    }    //表格栏位格式化显示    protected override void FormatColumns()    {        Column(c => c.Type).Select(new SelectOption { Codes = AppDictionary.GoodsType });        Column(c => c.TaxRate).Template((b, r) => b.Text(r.TaxRate?.ToString("P")));    }    public void New() => ShowForm();//新增按钮方法    public void DeleteM() => OnDeleteM(Client.Goods.DeleteGoodsesAsync);//批量删除按钮方法    public void Edit(KmGoods row) => ShowForm(row);//编辑操作方法    public void Delete(KmGoods row) => OnDelete(row, Client.Goods.DeleteGoodsesAsync);//删除操作方法}
2.2. 创建Form页面在KIMS.Razor项目BaseData\Forms文件夹下创建GoodsForm类该类是数据编辑和查看明细页面,继承WebForm
[Dialog(800, 420)]//设置对话框大小class GoodsForm : WebForm{    //表单布局    protected override void BuildFields(FieldBuilder builder)    {        builder.Hidden(f => f.Id);//隐藏字段        builder.Table(table =>        {            table.ColGroup(15, 35, 15, 35);            table.Tr(attr =>            {                builder.Field(f => f.Code).Enabled(TModel.IsNew).Build();//编码,编辑时灰显                builder.Field(f => f.Name).Build();            });            table.Tr(attr =>            {                builder.Field(f => f.Unit).Set(f => f.Codes, AppDictionary.GoodsUnit).Build();            });            table.Tr(attr => builder.Field(f => f.Model).ColSpan(3).Build());            table.Tr(attr => builder.Field(f => f.TaxRate).ColSpan(3).Set(f => f.Items, AppDictionary.TaxRates).Build());//单选按钮            table.Tr(attr =>            {                builder.Field(f => f.MinStock).Build();//数值框                builder.Field(f => f.MaxStock).Build();            });            table.Tr(attr => builder.Field