question

Shri-6058 avatar image
0 Votes"
Shri-6058 asked AgaveJoe commented

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

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>
}
}
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.

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,

I want to confirm with you that the question is transfer the checkbox list checked value to the controller, right?

And can you share the relates class definition, such as the TimeSheetViewModel, TimeSheet model, TimeEntry and the DayList, so that we can know which properties they have.

0 Votes 0 ·

1 Answer

ZhiLv-MSFT avatar image
0 Votes"
ZhiLv-MSFT answered AgaveJoe commented

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


image.png (178.7 KiB)
1.gif (1.6 MiB)
· 8
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.

Thank you @ZhiLv-MSFT for the great sample. This has a "submit" button to carry the checkbox bool values to process. In my case requirement is, I need to submit the form everytime when I click the checkbox (bring true to controller) via ajax postback. Thats the reason on click jquery/ajax based submit postback. I tried the following but it doesnt work.

The reason I need to submit after every checkbox(select or unselect) reload the page is because I am processing some calculations (when employee select waivemealtime, I need to deduct from the Meal hour and process total hour etc. simillar to the screen attached,
will you please let me kow how I can pass the checked/unchecked checkbox from $post() and reload the page after success?




190380-untitled.jpg


0 Votes 0 ·
untitled.jpg (35.1 KiB)

190407-untitled.jpg



0 Votes 0 ·
untitled.jpg (105.8 KiB)

This has a "submit" button to carry the checkbox bool values to process. In my case requirement is, I need to submit the form everytime when I click the checkbox (bring true to controller) via ajax postback. Thats the reason on click jquery/ajax based submit postback. I tried the following but it doesnt work.

The reason I need to submit after every checkbox(select or unselect) reload the page is because I am processing some calculations (when employee select waivemealtime, I need to deduct from the Meal hour and process total hour etc. simillar to the screen attached,
will you please let me kow how I can pass the checked/unchecked checkbox from $post() and reload the page after success?

I understand you, but in my sample, I just to do the post back action via the checkbox, instead of click submit type button. You could recheck it.

About the $(".missed").click not working, try to use F12 developer Console tools to check whether there has some JS error, or you could set some break point in the JS click event and the MVC controller action, then debug your code.

0 Votes 0 ·

form collection hidden field shows as first true, and then it turns into false.
190665-untitled.jpg




I dont know why it makes true first and then turn into false resulting no accurate state passed to controller.
Any ideas?

0 Votes 0 ·
untitled.jpg (40.5 KiB)
Show more comments