mvcChecklist list Add one row upon one checkbox selction instead of AddRange

Shri-6058 326 Reputation points
2022-04-05T22:41:05.417+00:00

I have a checkbox populated based on the List “DayList”, in Timesheet.cshtml

@if (Model.TimeSheet.DayList != null)
{
for (var i = 0; i < Model.TimeSheet.DayList.Count(); i++)
{
@azzedinehtmlsql .HiddenFor(x => Model.TimeSheet.DayList[i].IsMealBreakAllowed)
<div class="checkbox">
<label>@azzedinehtmlsql .CheckBoxFor(m => Model.TimeSheet.DayList[i].IsMealBreakWaived, new {
@class = "missed" })
@azzedinehtmlsql .FormatDayOfTheWeek(Model.TimeSheet.DayList[i].TRAN_DATE)
@azzedinehtmlsql .HiddenFor(x => Model.TimeSheet.DayList[i].TRAN_DATE)
</label>
</div>
}
}
It displays similar to the screen below:
Tuesday, 4/05
Thursday, 4/07
Friday, 4/08

On clicking the each/all checkbox(es), I want to perform postback and refresh full page after calculations and it goes to Homecontroller
$(".missed").click(function () {
$.post("/Home/Calculate", $("form").serialize(), function (json) {
TODO - perform a page refresh/postpack
});
});

POSTBack home ActionControl:

public ActionResult Calculate(TimeSheetViewModel model)
{
model.TimeSheet.EmpNo = User.EmpNo;
PayRollService.AddTimeEntries(model.TimeSheet);
return View("TimeSheet", model);
}

I want to add in my local database when checkbox is selected from AddTimeEntries(model.TimeSheet) and refresh the page. When checkbox is unchecked, it should be removed from the database(probably I just have to set day.IsMealBreakAllowed=false to ensure that any query won’t bring on to page) and refresh the page. However 1st time the table is empty it only allows to add records in the database. Will you please help me to correct my function shown below?

Currently my AddTimeEntries updates all checkboxes even if I only select one checkbox as I am adding via Timeentry list AddRange.

public bool AddTimeEntries(WeekClass week)
{
try {
var times = new List<Time>();
var now = DateTime.UtcNow;
using (var dbContext = new TimeSheetNEEntities())
{
foreach (var day in week.DayList)
{
var check = dbContext.Times.Where(x => x.EmployeeID == week.Emp_Uno && x.Date == day.TRAN_DATE).FirstOrDefault();
if (check == null)
{
times.Add(new Time
{
Date = day.TRAN_DATE,
DateCreated = now,
EmployeeID = week.Emp_Uno,
RestHours = day.Rest_Credit,
MealHours = day.Meal_Credit,
});
}
if(check != null)
{
dbContext.Times.Attach(check);
dbContext.Entry(check).CurrentValues.SetValues(times);
}
}

                }  
                dbContext.Times.AddRange(times);  
                dbContext.SaveChanges();  
            }  
        }  
        catch (Exception ex)  
        {  
            Console.WriteLine(ex.Message);  
        }  

        return true;  
    }  

I just want to save one record of day list when I select one checkbox. If the selected checkbox already exist, this record just has to be updated with datetime stamp (dbContext.Entry(check).CurrentValues.SetValues(times);)

Will you please assist me correcting this code?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,156 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,251 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,233 questions
{count} votes