2017-04-26 72 views
1

在我的asp.net核心代码中,我在验证中使用了ModelState.AddModelError(),并且它导致多线程并且代码将重复记录插入到表中。有没有办法来防止这个问题?谢谢。使用导致多线程和重复记录的ModelState.AddModelError插入

控制器:

public IActionResult BudgetAdd(BudgetDocumentAddViewModel model) 
{ 
    if ((model.ACCT.Length < 6) || (((model.FUND == "508") || (model.FUND == "760") || (model.FUND == "761")) && (model.ACCT.Substring(0, 2) != "50"))) 
    { 
     ModelState.AddModelError("ACCT", "Error: Incorrect Account (ACCT) Number!!!"); 
    } 
    if (ModelState.IsValid) 
    { 
     var document = new tDocument(); 
     document.FY = model.FY; 
     document.FUND = model.FUND; 
     document.AUTH = model.AUTH; 
     document.DEPT = model.DEPT; 
     document.DOCU_TYPE = model.DOCU_TYPE; 
     //Need to get the largest DOCU_ID. Cannot rely on the DOCU_ID from the form. 
     //document.DOCU_ID = model.DOCU_ID; 
     document.DOCU_ID = _docManager.GetDocuID(model.FY, model.FUND, model.AUTH, model.DEPT, model.DOCU_TYPE); 
     document.ENT_DT = model.ENT_DT; 
     document.BUD_YR = model.BUD_YR; 
     document.ORG = model.ORG; 
     document.ACCT = model.ACCT; 
     document.DOCU_DT = model.DOCU_DT; 

     if (model.DEPT_OBJ.Length >= 3) 
     { 
      document.OBJ = (model.DEPT_OBJ).Substring(0, 3); 
      if (model.DEPT_OBJ.Length == 5) 
      { 
       document.SUB_OBJ = (model.DEPT_OBJ).Substring(3, 2); 
      } 
     } 
     else 
     { 
      document.OBJ = ""; 
      document.SUB_OBJ = ""; 
     } 
     document.DESP = model.DESP; 
     //document.AMOUNT = model.AMOUNT.ToString().Replace(",", string.Empty);    
     var sAmount = model.AMOUNT.ToString(); 
     sAmount = sAmount.Replace(",", string.Empty); 
     document.AMOUNT = Decimal.Parse(sAmount); 
     document.RECV_DT = model.RECV_DT; 
     document.OUT_DT = model.OUT_DT; 

     if (model.FUND == "100" && model.ACCT == "002120") 
     { 
      document.OBJ = "212"; 
     } 
     if (model.FUND == "100" && model.ACCT == "002130") 
     { 
      document.OBJ = "213"; 
     } 
     if (model.FUND == "100" && model.ACCT == "003030") 
     { 
      document.OBJ = "605"; 
     } 
     if (model.FUND == "100" && model.ACCT == "003040") 
     { 
      document.OBJ = "304"; 
     } 
     if (model.FUND == "100" && model.ACCT == "003090") 
     { 
      document.OBJ = "609"; 
     } 
     if (model.FUND == "100" && model.ACCT == "004430") 
     { 
      document.OBJ = "607"; 
     } 
     if (model.FUND == "100" && model.ACCT == "006010") 
     { 
      document.OBJ = "601"; 
     } 
     if (model.FUND == "100" && model.ACCT == "006020") 
     { 
      document.OBJ = "602"; 
     } 
     if (model.FUND == "100" && model.ACCT == "007300") 
     { 
      document.OBJ = "730"; 
     } 
     //To get the username from the AspNetUsers table and assign it to the User_ID field in the tDocuments table. 
     document.USER_ID = User.Identity.Name.ToString(); 

     _docManager.AddDocument(document); 

     return RedirectToAction("BudgetSearch", "Document", new { FY = document.FY, FUND = document.FUND, DEPT = document.DEPT, AUTH = document.AUTH }); 
    } 

    model.Divisions = _docManager.GetOrganizations(); 
    return View(model); 
} 

的观点:

@model test.ViewModels.BudgetDocumentAddViewModel 
@{ 
    ViewBag.Title = "Budget Add Document"; 
} 
@using (Html.BeginForm("BudgetAdd", "Document", FormMethod.Post)) 
{ 
    <div class="panel panel-primary"> 
     <div class="panel-heading"> 
      <h1 class="panel-title">Add Document</h1> 
     </div>   
     <div asp-validation-summary="All" class="text-danger"></div> 
     <div class="panel-body"> 
      <div class="form-group col-lg-4 col-sm-6"> 
       @Html.LabelFor(m => m.DOCU_TYPE, "Document Type", new { @class = "labelRequired" }) 
       @Html.TextBox("DOCU_TYPE", "AL", new { @class = "form-control", @readonly = "readonly" }) 
      </div> 
      <div class="form-group col-lg-4 col-sm-6"> 
       @Html.LabelFor(m => m.AUTH, "Authority", new { @class = "labelRequired" }) 
       @Html.TextBox("AUTH", (string)ViewBag.AUTH, new { @class = "form-control", @readonly = "readonly" }) 
      </div> 
      <div class="form-group col-lg-4 col-sm-6"> 
       @Html.LabelFor(m => m.FY, "Fiscal Year", new { @class = "labelRequired" }) 
       @Html.TextBox("FY", (string)ViewBag.FY, new { @class = "form-control", @readonly = "readonly" }) 
      </div> 

      <div class="form-group col-lg-4 col-sm-6"> 
       @Html.LabelFor(m => m.DEPT, "Department", new { @class = "labelRequired" }) 
       @Html.TextBox("DEPT", (string)ViewBag.DEPT, new { @class = "form-control", @readonly = "readonly" }) 
      </div> 
      <div class="form-group col-lg-4 col-sm-6"> 
       @Html.LabelFor(m => m.DOCU_ID, "ID") 
       @*@Html.TextBox("DOCU_ID", (string)ViewBag.DOCU_ID, new { @class = "form-control"})*@ 
       <input type="text" id="DOCU_ID" name="DOCU_ID" value="@ViewBag.DOCU_ID" class="form-control" readonly="readonly" /> 
      </div> 
      <div class="form-group col-lg-4 col-sm-6"> 
       @Html.LabelFor(m => m.ENT_DT, "Entry Date") 
       @Html.TextBox("ENT_DT", @DateTime.Now.ToString("MM/dd/yyyy"), new { @class = "form-control", @readonly = "readonly" }) 
      </div> 

      <div class="form-group col-lg-4 col-sm-6"> 
       @Html.LabelFor(m => m.DOCU_DT, "Document Date") 
       @Html.TextBoxFor(m => m.DOCU_DT, new { @class = "form-control" }) 
      </div> 
      <div class="form-group col-lg-4 col-sm-6"> 
       @Html.LabelFor(m => m.BUD_YR, "Budget Year") 
       @Html.TextBox("BUD_YR", (string)ViewBag.FY, new { @class = "form-control" }) 
      </div> 
      <div class="form-group col-lg-4 col-sm-6"> 
       @Html.LabelFor(m => m.ORG, "Organization", new { @class = "labelRequired" }) 
       @Html.DropDownListFor(m => m.ORG, new SelectList(Model.Divisions, "ORGNUM", "ORGNUM"), "Select one", new { @class = "form-control" }) 
      </div> 

      <div class="form-group col-lg-4 col-sm-6"> 
       @Html.LabelFor(m => m.FUND, "Fund", new { @class = "labelRequired" }) 
       @Html.TextBox("FUND", (string)ViewBag.FUND, new { @class = "form-control", @readonly = "readonly" }) 
      </div> 
      <div class="form-group col-lg-4 col-sm-6"> 
       @Html.LabelFor(m => m.ACCT, "Acct", new { @class = "labelRequired" }) 
       @Html.TextBoxFor(m => m.ACCT, new { @class = "form-control capitalize" }) 
       @Html.ValidationMessageFor(m => m.ACCT, "", new { @class = "text-danger" }) 
      </div> 
      <div class="form-group col-lg-4 col-sm-6"> 
       @Html.LabelFor(m => m.DEPT_OBJ, "Dept. Obj") 
       @Html.TextBoxFor(m => m.DEPT_OBJ, new { @class = "form-control" }) 
       <br /><span>(DEPT. OBJ = OBJ + SUB OBJ - EX. 33450)</span> 
      </div> 

      <div class="col-lg-12 col-sm-12"> 
       @Html.LabelFor(m => m.DESP, "Description") 
       <div class="form-group"> 
        @Html.TextAreaFor(m => m.DESP, new { @class = "form-control capitalize", @rows = "4"})      
       </div>     
      </div> 


      <div class="form-group col-lg-4 col-sm-6"> 
       @Html.LabelFor(m => m.AMOUNT, "Amount") 
       <div class="input-group input-group col-lg-10 col-sm-11"> 
        <span class="input-group-addon">$</span> 
        @Html.TextBoxFor(m => m.AMOUNT, new { @class = "form-control auto" }) 
       </div> 
      </div> 
      <div class="form-group col-lg-4 col-sm-6"> 
       @Html.LabelFor(m => m.RECV_DT, "Document Date Recd") 
       @Html.TextBoxFor(m => m.RECV_DT, new { @class = "form-control" }) 
      </div> 
      <div class="form-group col-lg-4 col-sm-6"> 
       @Html.LabelFor(m => m.OUT_DT, "Date to Atcg/Div") 
       @Html.TextBoxFor(m => m.OUT_DT, new { @class = "form-control" }) 
      </div> 
     </div> 

     <div class="panel-footer"> 
      <div class="row"> 
       <div class="col-sm-12"> 
        <button class="btn btn-sm btn-primary" id="documentSave"><i class="glyphicon glyphicon-save"></i> Save</button> 
        &nbsp;&nbsp; 
        <button class="btn btn-sm btn-primary" id="documentClear"> Clear</button> 
       </div> 
      </div> 
     </div> 
    </div> 
} 

@section scripts{ 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $('#DOCU_DT').datepicker(); 
      $('#RECV_DT').datepicker(); 
      $('#OUT_DT').datepicker(); 
      $('.auto').autoNumeric('init'); 
      if ($('#ORG').val() != "" && ($('#ACCT').val()).length >= 6) 
       $('#documentSave').removeAttr("disabled"); 
      else 
       $('#documentSave').attr("disabled", true); 

     }); 
     $('#ORG').change(function() { 
      if ($('#ORG').val() != "" && ($('#ACCT').val()).length >= 6) 
       $('#documentSave').removeAttr("disabled"); 
      else 
       $('#documentSave').attr("disabled", true); 
     }); 
     $('#ACCT').change(function() { 
      if ($('#ORG').val() != "" && ($('#ACCT').val()).length >= 6) 
       $('#documentSave').removeAttr("disabled"); 
      else 
       $('#documentSave').attr("disabled", true); 

     }); 

     $('#documentClear').click(function() { 
      $('#DOCU_DT').val(""); 
      $('#BUD_YR').val(""); 
      $('#ORG').val(""); 
      $('#ACCT').val(""); 
      $('#DEPT_OBJ').val(""); 
      $('#DESP').val(""); 
      $('#AMOUNT').val(""); 
      $('#RECV_DT').val(""); 
      $('#OUT_DT').val(""); 
      return false; 
     }); 
     $('#documentSave').click(function() { 

      if ($('#ORG').val() != "") { 
       $('form').submit(); 
       return true; 
      } 
      else 
       return false; 
     }); 
    </script> 
} 

每次运行的代码,它插入相同的记录两次。如果注释掉ModelState.AddModelError()行,那么它没有任何问题(这意味着它插入一条记录)正常工作。

+0

什么是ACCT?它怎么能<5(数字)并且同时支持'substring'(字符串)? –

+0

它应该是model.ACCT.Length <5.我尝试通过放入ModelState.IsValid的检查来重新排列代码,但它不起作用。 –

+0

我注意到,当我使用IE11测试应用程序时,发生重复记录。但是,当我使用Firefox进行测试时,重复没有发生。如果仅在特定浏览器中出现重复记录,可能会导致重复记录插入表中。 –

回答

0

很可能是:

$('#documentSave').click(function() { 

     if ($('#ORG').val() != "") { 
      $('form').submit(); 
      return true; 
     } 
     else 
      return false; 
    }); 

#documentSave是表单内按钮;根据HTML 5 W3C specstype,“缺失值默认值是提交按钮状态”。所以,你的按钮就像一个提交按钮,你也在你的点击事件中调用$('form').submit() - 因此双提交。

当Firefox试图发布并重定向到表单动作时,Firefox可能会杀死基于javascript的提交,这是唯一可以解释为什么它只发生在IE中的解释。但它在不同浏览器中表现不同的事实尖叫着“客户端JavaScript问题”。

尝试将type="button"添加到您的按钮。

+0

谢谢保罗。这是答案。将类型='按钮'添加到保存按钮解决了问题。 –