学海无涯

导航

RedirectToAction 导航

插入数据后,导航到 GetByName 方法:

 

[HttpPost("Create")]
        public async Task<ActionResult> Create()
        {
            User u1 = new User { Name = "杨中科" };
            Leave leave = new Leave();
            leave.Requester = u1;
            leave.From = new DateTime(2021, 8, 8);
            leave.To = new DateTime(2021, 8, 9);
            leave.Remarks = "家里装修,回家处理";
            leave.Status = 0;
            await _context.Users.AddAsync(u1);
            await _context.Leaves.AddAsync(leave);
            await _context.SaveChangesAsync();
            return RedirectToAction("GetByName", new {name="杨中科"});//name为参数
        }
        [HttpGet("{name}", Name = "GetByName")]//Name指方法名,以便用于导航
        public async Task<ActionResult<IEnumerable<Leave>>> GetByName(string name)
        {
            User user = await _context.Users.SingleAsync(m => m.Name == name);
            var list = await _context.Leaves.Where(m => m.Requester.Id == user.Id).ToListAsync();
            return Ok(list);
        }

  

posted on 2022-09-29 16:15  宁静致远.  阅读(37)  评论(0编辑  收藏  举报