MVC - Checkbox list value not passed home controller with existing list

Shri-6058 326 Reputation points
2022-04-05T22:07:43.897+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>
}
}
On clicking the 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;
//var week = _payRollService.ExecutePayRollRules("", null, null); // TODO
foreach (var day in timeSheet.DayList)
{
If(day.IsMealBreakAllowed) // It always return false and even if I checked checkbox,
//it shows false. How can I ensure that the checkbox value
//true when checkbox is checked
{
PayRollService.AddTimeEntries(model.TimeSheet);
}

        return View("TimeSheet", model);  

}

I have a _PayrollService model called “TimeEntry.cs” has the

public bool IsMealBreakWaived { get; set; }

Do I have to have a separate property in the TimesheetView Model as well?

Please advise.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,157 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,252 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,234 questions
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,011 Reputation points Microsoft Vendor
    2022-04-06T03:25:34.903+00:00

    Hi @Shri-6058 ,

    MVC - Checkbox list value not passed home controller with existing list

    To this issue, you could refer the following sample:

    Models:

    public class TimeSheetViewModel  
    {  
        public string Name { get; set; }  
        public TimeSheet TimeSheet { get; set; }  
    }  
    
    public class TimeSheet  
    {  
        public string EmpNo { get; set; }  
        public List<TimeEntry> DayList { get; set; }  
    }  
    public class TimeEntry  
    {  
        public bool IsMealBreakWaived { get; set; }  
        public string TRAN_DATE { get; set; }  
        public bool IsMealBreakAllowed { get; set; }  
    }  
    

    Home Controller:

        public IActionResult TimesheetIndex()  
        {  
           //test data  
            var item = new TimeSheetViewModel()  
            {  
                Name = "A",  
                TimeSheet = new TimeSheet()  
                {  
                    EmpNo = "n101",  
                    DayList = new List<TimeEntry>() {  
                        new TimeEntry() { IsMealBreakWaived = true, IsMealBreakAllowed = false, TRAN_DATE = DateTime.Now.ToShortDateString() },  
                        new TimeEntry() { IsMealBreakWaived = false, IsMealBreakAllowed = true, TRAN_DATE = DateTime.Now.AddDays(1).ToShortDateString() },  
                        new TimeEntry() { IsMealBreakWaived = true, IsMealBreakAllowed = false, TRAN_DATE = DateTime.Now.AddDays(2).ToShortDateString() },  
                    }  
                }  
            };  
    
            return View(item);  
        }  
        [HttpPost]  
        public IActionResult Calculate(TimeSheetViewModel model)  
        {  
            return Ok("success");  
        }  
    

    The TimesheetIndex.cshtml page: we can get the checkbox value from the IsMealBreakWaived property.

    190387-image.png

    After running the application, the result is like this:

    190358-1.gif


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion


0 additional answers

Sort by: Most helpful