如何确定模型是否有效?

时间:2019-05-24 14:11:14

标签: c# asp.net-mvc lambda

我在这里遇到麻烦。这种情况是在更新现有记录的同时,如果没有记录,则首先搜索表以保存新行。

当我保存新记录时,没有问题。但是,当我尝试更新现有的时,没有任何更改。我试图在执行此操作的代码上专门放置一些断点。它只是在if(!ModelState.IsValid)行上结束。

注意: 为了获得一些伪数据,其中一些行进行了硬编码。

用于从数据库检索一些数据以传递给视图模型的功能

[HttpGet]
public ActionResult CardNumberAssignment(string empId, int cardNumberId)
{
    var getIdDetails = dbContext.CardNumberAssignments.Where(c => c.CardNumberId == cardNumberId && c.IsActive == true).SingleOrDefault();

    if (cardNumberId == 0) //EMPLOYEE HAS NO CARD NUMBER YET
    {
        var viewModel = new CardNumberQRCodeVM
        {
            CardNumberId = 0,
            CMId = empId,
            OldQRCode = "No QR Code history",
            OldReservedCardNumber = "No Card Number history",
            NewReservedCardNumber = GetRandomCardNumber()
        };

        return View(viewModel);
    }

    else
    {
        if (getIdDetails.CMId != empId) //JUST CHECKING IF THE EMPLOYEE HAS THE CORRECT CARDNUMBERID FROM DB
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        var empCardDetails = dbContext.CardNumberAssignments
                                      .Include(c => c.ReservedCardNumber)
                                      .Where(emp => emp.CMId == empId && emp.IsActive == true)
                                      .Select(fields => new CardNumberQRCodeVM
                                      {
                                          CardNumberId = fields.CardNumberId,
                                          CMId = empId,
                                          OldQRCode = fields.QRCode,
                                          IsActive = fields.IsActive,
                                          OldReservedCardNumber = fields.ReservedCardNumber.CardNumber,
                                      }).FirstOrDefault();

        empCardDetails.NewReservedCardNumber = GetRandomCardNumber();

        return View(empCardDetails);
    }
}

添加/编辑记录的功能

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SaveIdInformation(CardNumberQRCodeVM vm)
{
    if (!ModelState.IsValid)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }

    // JUST ADDING/EDITING SOME CARD NUMBERS....
    else
    {
        if (vm.CardNumberId == 0) //INSERT A NEW ROW FOR NEW CARD NUMBER AND QR CODE
        {
            var newCardNumber = new ReservedCardNumber
            {
                CardNumber = vm.NewReservedCardNumber,
                IsActive = true,
                CreatedDate = DateTime.Now,
                CreatedBy = "Paolo",
                ModifiedDate = DateTime.Now,
                ModifiedBy = "Paolo"
            };

            dbContext.ReservedCardNumbers.Add(newCardNumber);

            var newIdInfo = new CardNumberAssignment
            {
                CardNumberId = newCardNumber.Id,
                QRCode = vm.NewQRCode,
                CMId = vm.CMId,
                IsActive = true,
                CreatedDate = DateTime.Now,
                CreatedBy = "Paolo",
                ModifiedDate = DateTime.Now,
                ModifiedBy = "Paolo"
            };

            dbContext.CardNumberAssignments.Add(newIdInfo);
        }

        else // EDIT EXISTING
        {
            var getEmployeeIdInDb = dbContext.CardNumberAssignments.Single(e => e.Id == vm.CardNumberId);

            getEmployeeIdInDb.ReservedCardNumber.CardNumber = vm.NewReservedCardNumber;
            getEmployeeIdInDb.QRCode = vm.NewQRCode;
            getEmployeeIdInDb.IsActive = true;
            getEmployeeIdInDb.ModifiedDate = DateTime.Now;
            getEmployeeIdInDb.ModifiedBy = "Paolo";
        }

        dbContext.SaveChanges();
    }

    return RedirectToAction("CMDetails", "Admin", new { @empId = vm.CMId });
}

查看模型

public class CardNumberQRCodeVM
{
    public int CardNumberId { get; set; }

    public string CMId { get; set; }

    [Display(Name = "Existing QR Code")]
    public string OldQRCode { get; set; }

    [Required]
    [Display(Name = "New QR Code")]
    public string NewQRCode { get; set; }

    [Display(Name = "Resigned Cast Member?")]
    public bool IsActive { get; set; }

    public string CreatedBy { get; set; }
    public DateTime CreatedDate { get; set; }
    public string ModifiedBy { get; set; }
    public DateTime ModifiedDate { get; set; }

    public ReservedCardNumber ReservedCardNumber { get; set; }

    [Display(Name = "Old Card Number")]
    public string OldReservedCardNumber { get; set; }

    [Display(Name = "New Card Number")]
    public string NewReservedCardNumber { get; set; }
}

0 个答案:

没有答案