.net core MVC Month schedule for punching in and out 无法使用 entityframework 在数据库中存储天数

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

我想生成一个包含一个月中所有日期的视图,如下图所示。 https://drive.google.com/file/d/1pJvxXeajDnL7_W4VT4CNAQy_lONhRwf9/view?usp=sharing

这是我的模型:

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

这是我的控制器

 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);
        }

这是我的观点:

@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>

当我创建表单时,我无法接收日值,如何将日值发送到数据库?谢谢。 此外,我从前端视图而不是使用控制器(后置)编写日期,但有没有更好的方法?如何使用控制器编码以其他方式基于 select 月份和年份创建正确的日期列表?

您没有在表单中设置“天”的值,因此您无法在控制器中获取它,请尝试:

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

我试过如下:

@{
    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>

在控制器中:

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();
        }

结果: