'.net core MVC Month schedule for punching in and out cant store days in database using entityframework

I want to generate a view with all days of a month like this image below. https://drive.google.com/file/d/1pJvxXeajDnL7_W4VT4CNAQy_lONhRwf9/view?usp=sharing

here is my model:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

#nullable disable

namespace abcs.Models
{
    public partial class PunchIn
    {
        public uint Pid { get; set; }
        public int? Year { get; set; }
        public int? Month { get; set; }
        [Range(1,31,ErrorMessage ="days range is from 1 to 31")]
        public int? Day { get; set; }
        [Display(Name = "punch in time")]
        [DataType(DataType.Time)]
        public DateTime? PunchIn1 { get; set; }
        [Display(Name = "puch out time")]
        [DataType(DataType.Time)]
        public DateTime? PunchOut { get; set; }
        [Display(Name = "remark")]
        public string Remark2 { get; set; }
    }
}

here is my controller

 public IActionResult Create()
        {
          return View();
        }
    
     [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Pid,Year,Month,Day,PunchIn1,PunchOut,Remark2")] PunchIn punchIn)
        {
            ViewBag.Year = punchIn.Year;


            if (ModelState.IsValid)
            {
                _context.Add(punchIn);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(punchIn);
        }

here is my view:

@model abcs.Models.PunchIn

@{
    ViewData["Title"] = "Create";
}
<style>
    tr {
        background-color: yellowgreen;
        color: white;
    }

    table, th, td {
        border: 2px solid black;
    }
</style>

<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>
            @{
                int year = 2022;
                for (int month = 1; month <= 1; month++)
                {
                        <h2 align="center">@year @month Month</h2>
                    int daysInMonth = DateTime.DaysInMonth(year, month);
                    for (int day = 1; day <= daysInMonth; day++)
                    {
                        DateTime weekday = new DateTime(year, month, day);

                        <tr>                           
                            <td>
                                <div class="form-group">
                                    @day
                                    <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 asp-for="PunchIn1" class="form-control" />
                                    <span asp-validation-for="PunchIn1" class="text-danger"></span>
                                </div>
                            </td>
                            <td>
                                <div class="form-group">
                                    <input asp-for="PunchOut" class="form-control" />
                                    <span asp-validation-for="PunchOut" class="text-danger"></span>
                                </div>
                            </td>
                            <td>
                                <div class="form-group">
                                    <input 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>

When I create a form, I can't recieve day value, how to send day value to database?Thank you. In addition, I write date from Front-end view instead of using controller(postend), but is there a better way?How can I create correct days list based on select month and year in other ways with controller coding?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source