locked
Some records saved twice strangely at published web app. only! RRS feed

  • Question

  • User991566988 posted

    Hi

    There is some strange issue that happend with me in my published mvc core 2.1 web app. at public ip server only, With Sql Server 2014. that is it doesn't happen difinitly at local DB and web app! (run by Visual Studio Professional 2019 locally)

    Some records somtimes saved twice at the database despite it is one insert process only by normal Entity Framework Core, When I look at insert time it is the same time exactly or differ only by a part of second like this:

    Holidays model:

    public partial class Holidays
        {
            public int Id { get; set; }
    
            public decimal CtznId { get; set; }
    
            public DateTime SDate { get; set; }
    
            public DateTime EDate { get; set; }
    
            public int TxtTime { get; set; }
    
            public double TxtDay { get; set; }
    
            public int? Centerd { get; set; }
    
            public bool BackStatus { get; set; }
    
            public string Notes { get; set; }
    
            public string InsertUser { get; set; }
            public DateTime InsertDate { get; set; }
    
            public Center Center { get; set; }
    
            public Ctzn Ctzn { get; set; }
            
        }

    Get and post Create method at HolidaysController:

        public class HolidaysController : Controller
        {
            private readonly ApplicationDbContext _context;
    
            public HolidaysController(ApplicationDbContext context)
            {
                _context = context;
            }
            // Some code

    public IActionResult Create()
    {
    var center = User.FindFirst("CenterId").Value; // got by user claim principal correctly.
    ViewData["CenterId"] = new SelectList(_context.Center, "Id", "CenterName", center);
    return View();
    }
    [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Create([Bind("Id,CtznId,SDate,EDate,TxtTime,TxtDay,CenterId,BackStatus,Notes,InsertUser,InsertDate")] Holidays holiday) { if (ModelState.IsValid) { holiday.InsertDate = DateTime.Now; _context.Add(holiday); await _context.SaveChangesAsync(); ViewBag.message = "Saved correctly"; } ViewData["CenterId"] = new SelectList(_context.Center, "Id", "CenterName", holiday.CenterId); return View(holiday); }

    Hoildyas/Create view:

    @model Holidays
    
    <form asp-action="Create">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="row">
            <div class="col-md-3">
                <label class="control-label">Citizen Id</label>
                <input type="text" asp-for="CtznId" class="form-control" />
            </div>
            <div class="form-group">
                <div class="col-md-4 ">
                    <label asp-for="CenterId" class="control-label">Center</label>
                    <select asp-for="CenterId" class="form-control"  asp-items="ViewBag.CenterId"></select>
                </div>
            </div>
        </div>
    
        <div class="row">
            <div class="col-md-4">
                <label asp-for="SDate" class="control-label">Start Date</label>
                <input asp-for="SDate" class="form-control"  value="@ViewBag.sd" />
                <span asp-validation-for="SDate" class="text-danger"></span>
            </div>
            <div class="col-md-4">
                <label asp-for="EDate" class="control-label">End</label>
                <input asp-for="EDate" class="form-control" value="@ViewBag.ed" />
                <span asp-validation-for="EDate" class="text-danger"></span>
            </div>
            <div class="col-md-4">
                <label asp-for="TxtTime" class="control-label">Hours number</label>
                <input asp-for="TxtTime" class="form-control"  value="@ViewBag.tt" />
                <span asp-validation-for="TxtTime" class="text-danger"></span>
            </div>
        </div>
        <div class="row">
            <div class="col-md-4">
                <label asp-for="TxtDay" class="control-label">Days number</label>
                <input asp-for="TxtDay" class="form-control" value="@ViewBag.td" />
                <span asp-validation-for="TxtDay" class="text-danger"></span>
            </div>
            <div class="col-md-4">
                <div class="form-group">
                    <div class="checkbox">
                        <label>
                            <input asp-for="BackStatus" /> @Html.DisplayNameFor(model => model.BackStatus)
                        </label>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <div class="form-group">
                    <label asp-for="Notes" class="control-label"></label>
                    <input asp-for="Notes" class="form-control" />
                    <span asp-validation-for="Notes" class="text-danger"></span>
                </div>
            </div>
        </div>
    
        <div class="form-group">
            <input type="submit" value="Save" class="btn btn-success" />
        </div>
    </form>
    @if (!string.IsNullOrEmpty(ViewBag.message))
    {
    
            <div id="myAlert" class="alert alert-success collapse">
                <a href="#" class="close" data-dismiss="alert">&times;</a>
                @ViewBag.message
            </div>
        <script>
            $('#myAlert').show('fade');
        </script>
        <META HTTP-EQUIV='Refresh' Content='2; URL=Create' />
    
    }
    
    
    @section Scripts {
        @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
    }
    
    
    

    Now when the user insert records at Holidays model it is saved twice (like the images above) and happenned sometimes once say every for 20 or 30 records.

    Why? and how to solve please?

    Thursday, August 13, 2020 7:20 AM

Answers

  • User475983607 posted

    This behavior is commonly due to users double clicking the submit button.  Write code to ignore the second post or disable the button one the first click.  Google for source code.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 13, 2020 11:17 AM

All replies

  • User475983607 posted

    This behavior is commonly due to users double clicking the submit button.  Write code to ignore the second post or disable the button one the first click.  Google for source code.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 13, 2020 11:17 AM
  • User991566988 posted

    You are right.

    I did what you suggest. Thanks

    Sunday, August 16, 2020 6:14 AM