简体   繁体   English

表格被张贴两次

[英]Form being posted Twice

For Some Reason My Form is being posted twice.出于某种原因,我的表格被发布了两次。

// POST: MISObjects/Create
// To protect from overposting attacks, enable the specific properties you want to bind to, for 
// more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
 public async Task<IActionResult> Create([Bind("Id,Title,Subject,IncidentDate,DateRiased,Priroty,Category,Type,OMC_1,OMC_2,LastUpdatedBy,Description")] MISObject mISObject)
 {
        if (ModelState.IsValid) {
            _context.Add(mISObject);
            await _context.SaveChangesAsync();
            return RedirectToAction("Index");
        } else {
            var test = Json(new {
                status = "failure",
                formErrors = ModelState.Select(kvp => new { key = kvp.Key, errors = kvp.Value.Errors.Select(e => e.ErrorMessage) })
});  
           
}

}

My Form我的表格

 @using (Html.BeginForm("Create", "MISObjects", FormMethod.Post, new { @id = "myForm", @name = "myForm", enctype = "multipart/form-data" })) {      
  <div asp-validation-summary="All" class="text-danger"></div>

   @Html.HiddenFor(m => m.Id)
   @Html.TextBoxFor(model => model.Title, new { @class = "form-control", @placeholder = "Title" })
   @Html.ValidationMessageFor(m => m.Title, string.Empty, new { @style = "color:red;" })
   @Html.TextBoxFor(model => model.Subject, new { @class = "form-control", @placeholder = "Subject" })
   @Html.ValidationMessageFor(m => m.Subject, string.Empty, new { @style = "color:red;" })

   @Html.EditorFor(model => model.IncidentDated, new { htmlAttributes = new { @class = "form-control datetimepicker" } })
   @Html.ValidationMessageFor(model => model.IncidentDated)                      
   @Html.LabelFor(model => model.DateReported)
   @Html.EditorFor(model => model.DateReported, new { htmlAttributes = new { @class = "form-control datetimepicker" } })
   @Html.ValidationMessageFor(model => model.DateReported)
   <span asp-validation-for="Title" class="text-danger"></span>
   <input name="IsValid" type="hidden" value="@ViewData.ModelState.IsValid.ToString()" />
   <span asp-validation-for="Title" class="text-danger"></span>
   <div class="modal-footer">
   <button type="submit" id="AjaxPost" class="btn btn-primary">Save</button>
   <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
  </div>
} }

My Submit Ajax我的提交 Ajax

 <script type="text/javascript"> $(function () { $.validator.unobtrusive.parse('#myForm'); //Submit button var submitButton = $("#AjaxPost"); var infoForm = $("#myForm"); submitButton.click(function () { SubmitInfo(infoForm); }); }); function SubmitInfo(formContainer) { $.ajax({ url: "@Url.Action("Create", "MISObjects")", type: 'post', data: formContainer.serialize() }); } </script>

The reason was because I changed to begin form method for doing my ajax it was executing the post twice once I removed the below it was fine only one record was produced.原因是因为我更改为开始执行我的 ajax 的表单方法,一旦我删除了下面的内容,它就会执行两次帖子,它很好,只产生了一条记录。

<script type="text/javascript">
    $(function () {
        $.validator.unobtrusive.parse('#myForm');
        //Submit button
        var submitButton = $("#AjaxPost");
        var infoForm = $("#myForm");
        submitButton.click(function () {
            SubmitInfo(infoForm);
        });
    });

    function SubmitInfo(formContainer) {
        $.ajax({
            url: "@Url.Action("Create", "MISObjects")",
            type: 'post',
            data: formContainer.serialize()
        });
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM