。
这是我的模型:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有以形式设置“ day”的值,因此您无法将其放入控制器中,请尝试:
我尝试以下操作:
在控制器中:结果:
结果:

You didn't set the value of "day" in your form,so you couldn't get it in your controller ,try with:
I tried as below:
In controller:
Result:
