(十五)添加查询与编辑

1. 添加微软的行为类

image

2. 点击待办项弹出编辑侧栏

  • 找到代表每个待办项的gird块
    添加触发器,当左键点击待办时触发
<i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseLeftButtonUp">
        <i:InvokeCommandAction 
            CommandParameter="{Binding}"
            Command="{Binding DataContext.SelectedCommand,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ItemsControl}}" />
    </i:EventTrigger>
</i:Interaction.Triggers>
  • Mode类型是查找
  • 打开model代码新增命令SelectedCommand
    public DelegateCommand<ToDoDto> SelectedCommand { get; private set; }
  • 构造函数中新建
    SelectedCommand = new DelegateCommand<ToDoDto>(Selected);
  • 新增一个选中的对象来存放取到的数据
private ToDoDto currentDto;
//编辑选中对象/新增对象
public ToDoDto CurrentDto
{
    get { return currentDto; }
    set { currentDto = value; RaisePropertyChanged(); }
}
  • selected方法:查询
  private async void Selected(ToDoDto obj)
  {
      try
      {
          UpdateLoading(true);
          //为了选中数据库中的真实数据,需要通过服务查到数据库的真实数据
          var todoResult = await service.GetFirstOfDefaultAsync(obj.id);
          if (todoResult.Status)
          {
              CurrentDto = todoResult.Result;
              IsRightDrawerOpen = true;//右侧添加待办打开
          }
      }
      catch(Exception ex) 
      {
      }
      finally
      {
          UpdateLoading(false);
      }
  }
  • 显示到页面需要数据绑定
    image

3. 搜索功能模糊查询

  • 先修改API层
    image
  • 新建搜索条件
 private string search;

 public string Search//搜索条件
 {
     get { return search; }
     set { search = value; RaisePropertyChanged(); }
 }
  • 搜索框添加回车搜索事件,和通知更新双向绑定
    image
  • 命令集成一下:把addcommand修改为excutecommand
    image
  • 执行方法
  private void Execute(string obj)
  {
      switch(obj)
      {
          case "新增": Add();break;
          case "查询": GetDataAsync();break;
          case "保存":Save();break;
      }
  }
  • 页面绑定
    image
    image
    image
  • 修改getdata里面添加search

4. 保存(添加,更新)

  • 在add方法中添加
    CurrentDto=new ToDoDto();
  • 保存方法体
private async void Save()//执行保存数据的逻辑
{
    //首先判断是否符合条件
    if (string.IsNullOrWhiteSpace(CurrentDto.Title) || string.IsNullOrWhiteSpace(CurrentDto.Content)) return;
    UpdateLoading(true);

    try
    {
        if (CurrentDto.Id > 0)
        {//更新
            var updateResult = await service.UpdateAsync(CurrentDto);
            if (updateResult.Status)
            {
                var todo = ToDoDtos.FirstOrDefault(t => t.Id == CurrentDto.Id);
                if (todo != null)
                {
                    todo.Title = CurrentDto.Title;
                    todo.Content = CurrentDto.Content;
                    todo.Status = CurrentDto.Status;
                }
            }
            IsRightDrawerOpen = false;
        }
        else
        {//新增
            var addResult = await service.AddAsync(CurrentDto);
            if (addResult.Status)
            {
                ToDoDtos.Add(addResult.Result);
                IsRightDrawerOpen = false;
            }
        }
    }
    catch (Exception ex)
    {
    }
    finally
    {
        UpdateLoading(false);
    }
}

报错:更新保存的数据没有走数据库,debug发现response里面出错,content是400,
The 400 (Bad Request) status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
400(错误请求)状态代码表示服务器无法或处理请求。客户端错误(如畸形的请求语法、无效的请求信息格式或欺骗性的请求路由)。
发现是请求参数的格式错误
image

5. 修复 添加后无法再次编辑

API层todoservice,返回修改后的数据
image
备忘录的服务层也要修改

posted @ 2023-10-11 12:47  huihui不会写代码  阅读(66)  评论(0)    收藏  举报