question

Shri-6058 avatar image
0 Votes"
Shri-6058 asked ZhiLv-MSFT commented

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

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++)
{
@Html.HiddenFor(x => Model.TimeSheet.DayList[i].IsMealBreakAllowed)
<div class="checkbox">
<label>@Html.CheckBoxFor(m => Model.TimeSheet.DayList[i].IsMealBreakWaived, new {
@class = "missed" })
@Html.FormatDayOfTheWeek(Model.TimeSheet.DayList[i].TRAN_DATE)
@Html.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?

dotnet-csharpdotnet-aspnet-core-generaldotnet-aspnet-core-mvcdotnet-aspnet-mvc
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @Shri-6058,

  @Html.CheckBoxFor(m => Model.TimeSheet.DayList[i].IsMealBreakWaived, new {@class = "missed" })

First, from the view page code, we can see that you are binding the checkbox via the IsMealBreakWaived property, so, to get the checked value, you still need to use this property.

Second, in the AddTimeEntries(WeekClass week), you could use Linq where statement to filter the week data, then insert into or remove from database.

     public bool AddTimeEntries(WeekClass week)
     {
         try
         {
             //get the items need to insert into database.
             var insertdata = week.DayList.Where(c => c.IsMealBreakWaived == true).ToList();

             //insert into database.
             ...

             //get the items need to remove from the database
             var removedata = week.DayList.Where(c => c.IsMealBreakWaived == false).ToList();

             //remove records from the database 
             ...
         }
         
      catch (Exception ex)
      {
          Console.WriteLine(ex.Message);
      }
      return true;
   } 

Best regards,
Dillion

0 Votes 0 ·

0 Answers