将多个参数传递给控制器​​

时间:2020-07-01 13:23:15

标签: asp.net-mvc asp.net-core

好的,我有一个带有两个参数的方法。

public IActionResult Checked(DateTime fromDate, DateTime toDate)

我在视图中有一个按钮:

<form asp-action="Checked" asp-route-fromDate="2020-06-14" asp-route-toDate="2020-07-01" method="post">
                    <button type="submit" class="btn btn-info form-control"><i class="fas fa-plus"></i> &nbsp; All Checked</button>
                </form>

为简单起见,我对日期进行了硬编码。我得到的是一个没有触及该方法的404错误页面。

“找不到该网址的网页:https:// localhost:44377 / Admin / CurrentAccountCheck / Checked / 2020-06-14 / 2020-07-01”

我尝试添加:

[Route("Admin/CurrentAccountCheck/Checked/{fromDate?}/{toDate?}")]

甚至:

endpoints.MapControllerRoute(
                name: "CACC",
                pattern: "{area=Admin}/{controller=CurrentAccountCheck}/{action=Checked}/{fromDate?}/{toDate?}");
            endpoints.MapRazorPages();
启动文件中的

。 我在做什么错了?

我发现问题出在方法上:

            public IActionResult UpdateFlag(DateTime fromDate, DateTime toDate)
    {
        if (ModelState.IsValid)
        {
            var newfund = _db.Fund.Where(p => p.AccountId == 1).Where(p => p.Date >= fromDate).Where(p => p.Date <= toDate).ToList();
            foreach (var item in newfund)
            {
                Fund saveFund = new Fund { FundId = item.FundId };
                //_db.Attach(saveFund);
                saveFund.IsChecked = true;
                saveFund.CreatedBy = item.CreatedBy;
                saveFund.CreatedDate = item.CreatedDate;
                saveFund.LastModifiedBy = User.Identity.Name.ToString();
                saveFund.LastModifiedDate = DateTime.Now;

                _db.Update(saveFund);
            }
            
            _db.SaveChanges();
            ViewBag.Message = "Successfully Updated.";
            return RedirectToAction(nameof(Index), new { FromDate = fromDate, ToDate = toDate });
        }
        else
        {
             ViewBag.Message = "Failed ! Please try again.";
             return RedirectToAction(nameof(Index), new { FromDate = fromDate, ToDate = toDate });   
        }

    }

当我在_db.Update(saveFund)上放置一个断点时,我可以看到必需的数据,但是当我继续进行操作时,我得到了404-未找到页面,并且它不保存更改。

1 个答案:

答案 0 :(得分:0)

您必定会从SaveChanges中获取错误,因为当您使用entities获得var newfund = _db.Fund.Where(p => p.AccountId == 1).Where(p => p.Date >= fromDate).Where(p => p.Date <= toDate).ToList();时,dbcontext将对其进行跟踪。然后,您要创建新对象saveFund,以更新已经为entity的{​​{1}}。

要解决此问题,您可以对tracked个实体使用AsNoTracking()扩展方法。您也可以简化retrieving条件。使用如下代码。

Where

方法2 不要使用var newfund = _db.Fund.AsNoTracking() .Where(p => p.AccountId == 1 && p.Date >= fromDate && p.Date <= toDate) .ToList(); foreach (var item in newfund) { Fund saveFund = new Fund { FundId = item.FundId }; //_db.Attach(saveFund); saveFund.IsChecked = true; saveFund.CreatedBy = item.CreatedBy; saveFund.CreatedDate = item.CreatedDate; saveFund.LastModifiedBy = User.Identity.Name.ToString(); saveFund.LastModifiedDate = DateTime.Now; _db.Update(saveFund); } _db.SaveChanges(); ,而只需使用AsNoTracking() modify而不是创建新对象。您也不需要使用retrieved entities,因为_db.Update已经在entity中。

tracked
相关问题