MVC checkbox postback and refresh in the dashboard

Shri-6058 326 Reputation points
2022-03-22T03:37:26.103+00:00

I have an existing MVC application to read one external database to generate pdf upon submit button and its working fine.
TimeSheet.cshtml – has the following dashboard display:
I was asked to enhance application to insert some checkbox acknowledgement change based on checkbox selection postback on click refresh page. For example, if any day is missing Lunch start/end hour, they will have to select the respective checkbox or voluntarily skip (assuming lunch break was not taken) and submit. Since the application database is external, I don’t have control over that database. I have setup a local database to manage the following checkboxes
SELECT [ID]
,[EmployeeID]
,[Date]
,[MealHours]
,[RestHours]
,[DateCreated]
FROM [TimeSheet].[dbo].[Time]

Basically I need help on the following 3 functionalities:
(1) On click checkbox, postback local database and refresh the page
(2) ExecutePayRollRules logic to assign/call AddTimeEntries and save in local database. Whenever I select the respective week, it should automatically populate the checkbox values on load page.
(3) UI is available I just have to insert with other timesheet rules and add a row and all I need to integrate the values in the dashboard based on number of checkboxes selection. If no selection of checkboxes, no rows.

When any day is selected, the item should be saved in the database and refresh another row stating Lunch Hour (its default 1 hour and can’t change) for the specific date. There is last column called ‘Total’ automatically display ‘5’ if all 5 days checkboxes are selected or ‘2’ if any 2 days checkbox selected and so on. However when each checkbox is selected, the page should be automatically refresh the dashboard. How can I club the postback options with existing timesheet? On clicking missing lunch, I am trying to call this postback on the same Timesheet.cshtml:
@if (Model.TimeSheet.DayList != null)
{
for (var i = 0; i < Model.TimeSheet.DayList.Count(); i++)
{
if((Model.TimeSheet.DayList[i].Total_Worked > 0) && (Model.TimeSheet.DayList[i].Total_Lunch <= 0))
{
@azzedinehtmlsql .HiddenFor(x => Model.TimeSheet.DayList[i].IslunchBreakAllowed)
<div class="checkbox">
<label>@azzedinehtmlsql .CheckBoxFor(m => Model.TimeSheet.DayList[i]. IslunchBreakWaived, new { @class = "missed" }) @azzedinehtmlsql .FormatDayOfTheWeek(Model.TimeSheet.DayList[i].TRAN_DATE) @azzedinehtmlsql .HiddenFor(x => Model.TimeSheet.DayList[i].TRAN_DATE)</label>
</div>
}
}
}

@section Scripts
{
<script>
$(".missed").click(function () {
$.post("/Home/Calculate", $("form").serialize(), function (json) {
// handle response
// TODO - perform a page refresh/postpack
});
});
}
On clicking, it goes to the following HomeController.cs however I am not able to create a rule/assign & integrate with existing Weekclass. When selected, save the database, when uncheck remove the data and postback option is required. Currently WeekClass timesheet brings all the external data for the week. All I need is I need to save/retrieve data from my local database to include with the timesheet class, so it will display in the dashboard.

public ActionResult Calculate(TimeSheetViewModel model) {
model.TimeSheet.EmployeeID = User.EmployeeID;
// var week = _payRollService.ExecutePayRollRules(model.TimeSheet.Emp_Uno, null, null); // TODO

        _payRollService.AddTimeEntries(model.TimeSheet);  

        WeekClass timeSheet = _payRollService.GetTimeSheet(User.EmployeeUno, model.SelectedPayRollWeek, model.tz);   // (.tz is just a timezone that’s working)  
        model.TimeSheet = timeSheet;  
  

        return View("TimeSheet", model);  
    }  

Currently _payRollService.AddTimeEntries(model.Timesheet) is null so it won’t save in the database. I need some business logic to add the week first before calling AddTimeEntries

_payRollService.cs has the following for retrieving:

public Time[] GetEntriesFromCustomDb(int emplUno, DateTime fromDate, DateTime toDate)
{
using(var dbContext = new TimeSheetNEEntities())
{
var times = dbContext.Times
.Where(p=> p.EmployeeID == emplUno && (p.Date >= fromDate && p.Date <= toDate)) // TODO - fix, need payroll week
.ToArray();

            return times;  
        }   
    }  

//Adding or inserting checkbox values and save

public bool AddTimeEntries(WeekClass week)
{
try
{
var times = new List<Time>();
var now = DateTime.UtcNow;

            foreach(var day in week.DayList)  
            {   
                times.Add(new Time  
                {   
                    Date = day.TRAN_DATE,  
                    DateCreated = now,  
                    EmployeeID = week.Emp_Uno,  
                    RestHours = day.Rest_Credit,  
                    MealHours = day.Meal_Credit,  
                });  
            }  

            using(var dbContext = new TimeSheetEntities())  
            {  
                dbContext.Times.AddRange(times);    
                dbContext.SaveChanges();  
            }               
        }  
        catch(Exception ex)  
        {  
            // TODO log   
            //throw;  
        }     

        return true;  
    }  
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,280 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Shri-6058 326 Reputation points
    2022-03-22T03:39:26.587+00:00

    185299-timesheet.jpg

    0 comments No comments