检查但允许在Db中重复条目

时间:2017-08-10 00:28:23

标签: asp.net-mvc

我的Db允许重复的纪念碑,因此检查重复的传统方法将不起作用。用户首先输入一个纪念碑名称,然后我对Db进行检查,如果没有找到重复项,那么很好,允许用户输入其余的数据。如果找到一个或多个具有相同名称的纪念碑,则显示已在Db中的纪念碑列表。如果这是一个真正具有相同名称的新纪念碑,请允许用户输入新纪念碑。

到目前为止我所拥有的:

         [Authorize]
      public ActionResult MonumentTitle(string battleRecordID, int callingRecordID)
      {
          ViewBag.monumentBattleRecID = battleRecordID; // ID of the battle
          ViewBag.battleName = getBattleName(battleRecordID);

          var vModel = new MonumentTitle();
          vModel.BattleRecID = ViewBag.monumentBattleRecID;
          vModel.BattleName = ViewBag.battleName;
          vModel.CallingRecID = callingRecordID;
          return View(vModel);

      }

      [HttpPost]
      [Authorize]
      public ActionResult MonumentTitle(MonumentTitle monumentTitle)
        {

            ViewBag.callingRecordID = monumentTitle.CallingRecID;
            ViewBag.battleName = monumentTitle.BattleName;
            ViewBag.MonumentName = monumentTitle.MonumentName;

            var NmonumentTitle = monumentTitle;
            ViewBag.battleName1 = NmonumentTitle.BattleName;
            var List_monument = from s in db.Monuments
                           where (s.MonumentStatus == "A" &&
                           s.MonumentBattleRecID == monumentTitle.BattleRecID &&
                           s.MonumentName == monumentTitle.MonumentName
                         )
                           select s;

            List_monument = List_monument.OrderBy(s => s.MonumentName);
            var vModel = new MonumentTitleDuplicate();
            vModel.MonumentTitle = NmonumentTitle;
            vModel.Monument = List_monument.ToList();


            if (vModel.Monument.Count == 0)
            {
                return RedirectToAction("MonumentCreate", new { battleRecord = vModel.MonumentTitle.BattleRecID, callingRecordID = vModel.MonumentTitle.CallingRecID, monumentName = vModel.MonumentTitle.MonumentName });
            }
            {
                return RedirectToAction("MonumentTitleDuplicate",  vModel );
            }

        }

适用于没有重复的情况。我遇到问题的地方是试图将控制权转换为" MonumentTitleDuplicate"并传递模型。

          [Authorize]
      [HttpGet]
      public ViewResult MonumentTitleDuplicate(MonumentTitleDuplicate monumentTitleDuplicate)
      {                  



          return View("MonumentTitleDuplicate", monumentTitleDuplicate);

      }

      [HttpPost]
      [Authorize]
      public ActionResult MonumentTitleDuplicate(MonumentTitle monumentTitle)
      {

          ViewBag.callingRecordID = monumentTitle.CallingRecID;
          return RedirectToAction("MonumentCreate", new { battleRecord = monumentTitle.BattleRecID, callingRecordID = monumentTitle.CallingRecID, monumentName = monumentTitle.MonumentName });

      }

    [Authorize]
    public ActionResult MonumentCreate(string battleRecord, int callingRecordID, string monumentName)
    {

1 个答案:

答案 0 :(得分:0)

得到它 - 需要使用TempData将模型传递给新控制器,所以:

          [Authorize]
      public ActionResult MonumentTitle(string battleRecordID, int callingRecordID)
      {
          ViewBag.monumentBattleRecID = battleRecordID; // ID of the battle
          ViewBag.battleName = getBattleName(battleRecordID);

          var vModel = new MonumentTitle();
          vModel.BattleRecID = ViewBag.monumentBattleRecID;
          vModel.BattleName = ViewBag.battleName;
          vModel.CallingRecID = callingRecordID;
          return View(vModel);

      }

      [HttpPost]

      [Authorize]
      public ActionResult MonumentTitle(MonumentTitle monumentTitle)
        {

            ViewBag.callingRecordID = monumentTitle.CallingRecID;
            ViewBag.battleName = monumentTitle.BattleName;
            ViewBag.MonumentName = monumentTitle.MonumentName;

            var NmonumentTitle = monumentTitle;
            ViewBag.battleName1 = NmonumentTitle.BattleName;
            var List_monument = from s in db.Monuments
                           where (s.MonumentStatus == "A" &&
                           s.MonumentBattleRecID == monumentTitle.BattleRecID &&
                           s.MonumentName == monumentTitle.MonumentName
                         )
                           select s;

            List_monument = List_monument.OrderBy(s => s.MonumentName);
            var vModel = new MonumentTitleDuplicate();
            vModel.MonumentTitle = NmonumentTitle;
            vModel.Monument = List_monument.ToList();


            if (vModel.Monument.Count == 0)
            {
                return RedirectToAction("MonumentCreate", new { battleRecord = vModel.MonumentTitle.BattleRecID, callingRecordID = vModel.MonumentTitle.CallingRecID, monumentName = vModel.MonumentTitle.MonumentName });
            }
            {
                TempData["monumentTitleDuplicate"] = vModel;
                return RedirectToAction("MonumentTitleDuplicate");
            }

        }


      [Authorize]
      public ActionResult MonumentTitleDuplicate()
      {

          MonumentTitleDuplicate monumentTitleDuplicate = TempData["MonumentTitleDuplicate"] as MonumentTitleDuplicate;

          return View(monumentTitleDuplicate);

      }

      [HttpPost]
      [Authorize]
      public ActionResult MonumentTitleDuplicate(MonumentTitle monumentTitle)
      {

          ViewBag.callingRecordID = monumentTitle.CallingRecID;
          return RedirectToAction("MonumentCreate", new { battleRecord = monumentTitle.BattleRecID, callingRecordID = monumentTitle.CallingRecID, monumentName = monumentTitle.MonumentName });

      }

可能要做一些清理工作(比如摆脱我试图使用ViewBag的东西),但似乎确实有效。