I tried to modify your codes as below:
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script>
$(function () {
// initialize the value of the input field to the selected value
$('.Month').val($('#selectlist').val());
$('#selectlist').change(function () {
// when the value of the dropdown list changes
// update the input field
var month = this.val;
$('.Month').val($(this).val());
//delete the unnecessary input and others
if (month != 1 && month != 3 && month != 5 && month != 7 && month != 8&& month != 10 && month != 12)
{
$(document).find("input[name='PunchIn[30].Remark2']").remove();
$(document).find("input[name='PunchIn[30].Month']").remove();
$(document).find("input[name='PunchIn[30].Day']").remove();
$(document).find("input[name='PunchIn[30].PunchIn1']").remove();
$(document).find("input[name='PunchIn[30].PunchOut']").remove();
$(document).find("a[name='31']").remove();
}
});
//you could try to add a input with append yourself
});
</script>
<h1>Create</h1>
<h4>PunchIn</h4>
<hr />
<select id="selectlist" asp-items=months></select>
<h2 align="center">@year @month Month</h2>
<form asp-action="Create">
<table class="table">
<thead>
<tr>
<th>
date
</th>
<th>
Punch in time
</th>
<th>
Puncn out time
</th>
<th>
remark
</th>
</tr>
</thead>
<tbody>
@for (int day = 1; day <= daysInMonth; day++)
{
<tr>
<td>
<div class="form-group">
<input name=PunchIn[@(day-1)].Month asp-for="Month" class="Month" value="" />
</div>
<div class="form-group">
<a name="@day">@day</a>
<input name=PunchIn[@(day-1)].Day asp-for="Day" class="form-control" value=@day hidden />
<span asp-validation-for="Day" value="@day" class="text-danger"></span>
</div>
</td>
<td>
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<input name=PunchIn[@(day-1)].PunchIn1 asp-for="PunchIn1" class="form-control" />
<span asp-validation-for="PunchIn1" class="text-danger"></span>
</div>
</td>
<td>
<div class="form-group">
<input name=PunchIn[@(day-1)].PunchOut asp-for="PunchOut" class="form-control" />
<span asp-validation-for="PunchOut" class="text-danger"></span>
</div>
</td>
<td>
<div class="form-group">
<input name=PunchIn[@(day-1)].Remark2 asp-for="Remark2" class="form-control" />
<span asp-validation-for="Remark2" class="text-danger"></span>
</div>
</td>
</tr>
}
<input type="submit" value="Create" class="btn btn-primary" />
</tbody>
</table>
</form>
Result:
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/z2Q82.gif
You didn't set the value of "day" in your form,so you couldn't get it in your controller ,try with:
<form asp-action="Create">
.........
<div class="form-group">
@day
<input asp-for="Day" class="form-control" value=@day hidden/>
<span asp-validation-for="Day" value="@day" class="text-danger"></span>
</div>
.......
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
I tried as below:
@{
int year = 2022;
int month = 3;
int daysInMonth = DateTime.DaysInMonth(year, month);
}
<h1>Create</h1>
<h4>PunchIn</h4>
<hr />
<h2 align="center">@year @month Month</h2>
<form asp-action="Create">
<table class="table">
<thead>
<tr>
<th>
date
</th>
<th>
Punch in time
</th>
<th>
Puncn out time
</th>
<th>
remark
</th>
</tr>
</thead>
<tbody>
@for (int day = 1; day <= daysInMonth; day++)
{
<tr>
<td>
<div class="form-group">
@day
<input name =PunchIn[@(day-1)].Day asp-for="Day" class="form-control" value=@day hidden />
<span asp-validation-for="Day" value="@day" class="text-danger"></span>
</div>
</td>
<td>
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<input name =PunchIn[@(day-1)].PunchIn1 asp-for="PunchIn1" class="form-control" />
<span asp-validation-for="PunchIn1" class="text-danger"></span>
</div>
</td>
<td>
<div class="form-group">
<input name =PunchIn[@(day-1)].PunchOut asp-for="PunchOut" class="form-control" />
<span asp-validation-for="PunchOut" class="text-danger"></span>
</div>
</td>
<td>
<div class="form-group">
<input name =PunchIn[@(day-1)].Remark2 asp-for="Remark2" class="form-control" />
<span asp-validation-for="Remark2" class="text-danger"></span>
</div>
</td>
</tr>
}
<input type="submit" value="Create" class="btn btn-primary" />
</tbody>
</table>
</form>
In controller:
public async Task<IActionResult> Create( List<PunchIn> PunchIn)
{
if (ModelState.IsValid)
{
var targetlist=PunchIn.Where(x => x.PunchIn1 != null).ToList();
_context.AddRange(targetlist);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View();
}
Result:
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/xMoar.gif