发布于 2025-01-31 11:25:57 字数 4840 浏览 2 评论 0原文

我想在下面的此图像中以一个月的整个天数生成视图。

这是我的模型:

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>

当我创建表单时,我无法收到日子价值,如何将日子价值发送到数据库?谢谢。 此外,我从前端视图而不是使用控制器(PostEnd)编写日期,但是有更好的方法吗?如何根据选择的月份和年来创建正确的日子列表,并通过其他方式进行控制器编码?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海蓝天 2025-02-07 11:25:58

您没有以形式设置“ day”的值,因此您无法将其放入控制器中,请尝试:

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

结果:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文