0

I am working on a website where I need to set the ApplicationStartDate & ApplicationEndDate. When I perform this operation, ApplicationStartDate displays correctly but ApplicationEndDate displays the value of previous record which should not be the case.

Here is the .cshtml file :

@model Derawala.Models.ViewModels.ParentForApply
@{
    ViewData["Title"] = "Set Application Period";
    Layout = "_Layout";
}

<h1 class="text-success mt-3"><i class="fas fa-calendar-week"></i> Set Date Range For Scholarship Acceptance :</h1>
<hr class="mx-n3" />
@if (ViewBag.IsDurationSet)
{
    <div class="alert alert-success alert-dismissible fade show" role="alert">
        <strong><i class="fas fa-check-circle"></i> Success!</strong> Application Acceptance Is Set From <b>@WC.AppStartDate.ToString("MMMM dd yyyy")</b> To <b>@WC.AppEndDate.ToString("MMMM dd yyyy")</b>
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
}
@if (WC.AppStartDate == DateTime.MinValue || WC.AppEndDate == DateTime.MinValue)
{

    <form method="post">
        <div class="row">
            <div class="col-md-2">
                <h4 class="text-primary p-2"><i class="fas fa-hand-point-right"></i> Start Date :</h4>
            </div>
            <div class="col-md-4">
                <input type="date" asp-for="@Model.AppPeriod.AppStartDate" class="form-control form-control-lg" required />
            </div>
            <div class="text-primary col-md-2">
                <h4 class="p-2"><i class="fas fa-hand-point-right"></i> End Date :</h4>
            </div>
            <div class="col-md-4">
                <input type="date" asp-for="@Model.AppPeriod.AppEndDate" class="form-control form-control-lg" required />
            </div>
        </div>
        <div class="row d-flex justify-content-center mt-3">
            <div class="col-md-2">
                <button type="submit" style="font-weight:bolder;" class="btn btn-success w-100 text-white mt-1">Set Duration <i class="fas fa-calendar-check"></i></button>
            </div>
            <div class="col-md-2">
                <button type="reset" style="font-weight:bolder;" class="btn btn-warning w-100 mt-1">Reset Date <i class="fas fa-calendar"></i></button>
            </div>
        </div>
    </form>
}
else
{
    <h3 class="text-primary"><u>Currently Set As</u> : </h3>
    <div class="row">
        <div class="col-md-2">
            <h4 class="text-danger p-2"><i class="fas fa-hand-point-right"></i> <u>Start</u> <u>Date</u> :</h4>
        </div>
        <div class="col-md-4">
            <h4 class="p-2">@WC.AppStartDate.ToString("dd-MM-yyyy")</h4>
        </div>
        <div class="col-md-2">
            <h4 class="text-danger p-2"> <i class="fas fa-hand-point-right"></i> <u>End</u> <u>Date</u> :</h4>
        </div>
        <div class="col-md-4">
            <h4 class="p-2">@WC.AppEndDate.ToString("dd-MM-yyyy")</h4>
        </div>
    </div>
    <br /><br />
        <form method="post">
            <div class="row">
                <div class="col-md-2">
                    <h4 class="text-primary p-2"><i class="fas fa-hand-point-right"></i> Start Date :</h4>
                </div>
                <div class="col-md-4">
                    <input type="date" asp-for="@Model.AppPeriod.AppStartDate" class="form-control form-control-lg" required />
                </div>
                <div class="text-primary col-md-2">
                    <h4 class="p-2"><i class="fas fa-hand-point-right"></i> End Date :</h4>
                </div>
                <div class="col-md-4">
                    <input type="date" asp-for="@Model.AppPeriod.AppEndDate" class="form-control form-control-lg" required />
                </div>
            </div>
            <div class="row d-flex justify-content-center mt-3">
                <div class="col-md-2">
                    <button type="submit" style="font-weight:bolder;" class="btn btn-success w-100 text-white mt-1">Set Duration <i class="fas fa-calendar-check"></i></button>
                </div>
                <div class="col-md-2">
                    <button type="reset" style="font-weight:bolder;" class="btn btn-warning w-100 mt-1">Reset Date <i class="fas fa-calendar"></i></button>
                </div>
            </div>
        </form>
        }

[HttpGet] Controller Method :

[HttpGet]
        public async Task <IActionResult> AppPeriod(bool IsSuccess=false)
        {
            ParentForApply ParentVM = new ParentForApply();
            int count = await _db.AppDuration.CountAsync();
            if (count == 0)
            {
                WC.AppStartDate = new DateTime();
                WC.AppEndDate = new DateTime();
            }
            else
            {
                WC.AppStartDate = await _db.AppDuration.MaxAsync(i => i.AppStartDate);
                WC.AppEndDate = await _db.AppDuration.MaxAsync(i => i.AppEndDate);
            }
            ViewBag.IsDurationSet = IsSuccess;
            return View();
        }

[HttpPost] Controller Method :

[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> AppPeriod(ParentForApply ParentVM)
        {
            int id = await _db.AppDuration.MaxAsync(i => i.Id);
            var data = await _db.AppDuration.FindAsync(id);
            _db.AppDuration.Remove(data);
            AppDurationPeriod app = new AppDurationPeriod()
            {
                AppStartDate = ParentVM.AppPeriod.AppStartDate,
                AppEndDate = ParentVM.AppPeriod.AppEndDate
            };
            WC.AppStartDate = app.AppStartDate;
            WC.AppEndDate = app.AppEndDate;

            await _db.AppDuration.AddAsync(app);
            await _db.SaveChangesAsync();
            return RedirectToAction("AppPeriod",new { IsSuccess = true});
        }

I am deleting the last inserted record and inserting new record in the table to use as the latest dates.

Database Column stores the correct values

Website displays the incorrect value of AppEndDate

When I debug the code, something phishy happens in this line of code where it changes the value of AppEndDate from original value of database to 30-06-2022

It can be seen in the images that, database has stored the correct values but website doesn't show the same value. In the second line of else block of the last image, something phishy happenes when I debug the code. Second line of that else block somehow changes the value of AppEndDate from the original value to 30-06-2022

1 Answer 1

1

This is because when you load the page, the largest data is fetched from the database table by default, you can change the HttpGet method you use to get the data like this:

[HttpGet]
        public async Task<IActionResult> AppPeriod(bool IsSuccess = false)
        {
            
            ParentForApply ParentVM = new ParentForApply();
            int count = await _db.AppDuration.CountAsync();
            if (count == 0)
            {
                WC.AppStartDate = new DateTime();
                WC.AppEndDate = new DateTime();
            }
            else
            {
                int id = await _db.AppDuration.MaxAsync(i => i.Id);
                var data = await _db.AppDuration.FindAsync(id);
                WC.AppStartDate = data.AppStartDate;
                WC.AppEndDate = data.AppEndDate;
                //WC.AppStartDate = await _db.AppDuration.MaxAsync(i => i.AppStartDate);
                //WC.AppEndDate = await _db.AppDuration.MaxAsync(i => i.AppEndDate);
            }
            ViewBag.IsDurationSet = true;
            return View();
        }

Test Result:

enter image description here enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.