英文:
How to save multiple rows of HTML table into database
问题
我在HTML表格行中保存每个记录到数据库方面遇到了困难
我想要将HTML表格中的一组记录保存到数据库中。我已经能够获取一些列的数据。然而,特定列(描述)的第一个记录通常会覆盖其余的行。到目前为止,我的代码如下:
ViewModel
public SelectList Meetings { get; set; }
public string MeetingId { get; set; }
public string Description { get; set; }
public bool IsPresent { get; set; }
public DateTime Date { get; set; }
public List<SelectListItem> Members { get; set; }
public string MemberId { get; set; }
View
@using (Html.BeginForm("AddAttendance","Attendance",FormMethod.Post))
{
<div class="card-body">
<div class="form-group">
<input type="date" asp-for="@Model.Date" class="form-control js-datepicker" asp-format="{0:dd/MMM/yyyy}">
</div>
<div class="form-group">
<select type="text" id="meetingType" class="select2 form-control" asp-items="@Model.Meetings" asp-for="@Model.MeetingId" style="width:100%" multiple></select>
</div>
<div class="table-responsive">
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@for (int i = 0; i < Model.Members.Count(); i++)
{
<tr>
<td>@Model.Members[i].Text</td>
<td><input asp-for="@Model.Description" id="desc"/></td>
<td>
<input type="hidden" class="custom-checkbox" asp-for="@Model.Members[i].Value" />
<label class="a8z-toggle-switch" data-size="sm" data-color="blue" data-text="true" data-style="rounded">
<input asp-for="Members[i].Selected" type="checkbox" id="example_default_1">
<span class="toggle">
<span class="switch"></span>
</span>
</label>
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
}
Controller
[HttpPost]
public IActionResult AddAttendance(AddAttendanceVM addAttendanceVM)
{
foreach (var att in addAttendanceVM.Members)
{
Attendance addThisAttendance = new()
{
Date = addAttendanceVM.Date,
Description = addAttendanceVM.Description,
MemberId = att.Value,
MeetingId = addAttendanceVM.MeetingId,
IsPresent = addAttendanceVM.IsPresent,
CreatedBy = User.Claims.FirstOrDefault(c => c.Type == "Name").Value,
CreatedDate = DateTime.Now
};
xct.Attendances.Add(addThisAttendance);
}
xct.SaveChanges();
return ViewComponent(nameof(AttendanceLists));
}
感谢您的帮助。谢谢。
英文:
I have difficulty in saving each record in HTML table rows into database
I want to save a list of records from HTML table into a database. I have been able to get data for some columns. However, the first record for a particular column(description) often overrides the remaining rows. My codes so far are below
ViewModel
public SelectList Meetings { get; set; }
public string MeetingId { get; set; }
public string Description { get; set; }
public bool IsPresent { get; set; }
public DateTime Date { get; set; }
public List<SelectListItem> Members { get; set; }
public string MemberId { get; set; }
View
@using (Html.BeginForm("AddAttendance","Attendance",FormMethod.Post))
{
<div class="card-body">
<div class="form-group">
<input type="date" asp-for="@Model.Date" class="form-control js-datepicker" asp-format="{0:dd/MMM/yyyy}">
</div>
<div class="form-group">
<select type="text" id="meetingType" class="select2 form-control" asp-items="@Model.Meetings" asp-for="@Model.MeetingId" style="width:100%" multiple></select>
</div>
<div class="table-responsive">
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@for (int i = 0; i < Model.Members.Count(); i++)
{
<tr>
<td>@Model.Members[i].Text</td>
<td><input asp-for="@Model.Description" id="desc"/></td>
<td>
<input type="hidden" class="custom-checkbox" asp-for="@Model.Members[i].Value" />
<label class="a8z-toggle-switch" data-size="sm" data-color="blue" data-text="true" data-style="rounded">
<input asp-for="Members[i].Selected" type="checkbox" id="example_default_1">
<span class="toggle">
<span class="switch"></span>
</span>
</label>
</td>
</tr>
}
</tbody>
</table>
</div>
controller
[HttpPost]
public IActionResult AddAttendance(AddAttendanceVM addAttendanceVM)
{
foreach (var att in addAttendanceVM.Members)
{
Attendance addThisAttendance = new()
{
Date = addAttendanceVM.Date,
Description = addAttendanceVM.Description,
MemberId = att.Value,
MeetingId = addAttendanceVM.MeetingId,
IsPresent = addAttendanceVM.IsPresent,
CreatedBy = User.Claims.FirstOrDefault(c => c.Type == "Name").Value,
CreatedDate = DateTime.Now
};
xct.Attendances.Add(addThisAttendance);
}
xct.SaveChanges();
return ViewComponent(nameof(AttendanceLists));
and this is the results in the database
any help will be appreciated.
Thank you.
答案1
得分: 1
HTML元素的名称应该是唯一的,并且您在循环中使用了asp-for="Model.Description"
。这会导致使用用户提供的第一个值。另外,您使用了模型的属性,而不是列表中的项目内部的属性。
您需要比SelectListItem
携带的属性更多的属性。创建一个新的类Member
,其中包含Description
属性。
public class Member
{
public bool IsSelected { get; set; }
public string Text { get; set; }
public string Value { get; set; }
public string Description { get; set; }
}
更改Members
属性的类型
public List<Member> Members { get; set; }
在此之后,这行代码
<td><input asp-for="@Model.Description" id="desc"/></td>
应该更改为
<td><input asp-for="@Model.Members[i].Description" id="desc"/></td>
以及这行代码
Description = addAttendanceVM.Description,
应该更改为
Description = att.Description,
您还需要重新初始化Members
,因为类型已经更改。
英文:
Name for each HTML element should be unique and you use asp-for="Model.Description"
in a loop. This results into using the first value user provides. Also you use a property from a model, not from the item inside a list
You need more properties than SelectListItem
carries. Create new class Member
with Description
property
public class Member
{
public bool IsSelected { get; set; }
public string Text { get; set; }
public string Value { get; set; }
public string Description { get; set; }
}
Change the type of Members
property
public List<Member> Members { get; set; }
After this, this line
<td><input asp-for="@Model.Description" id="desc"/></td>
Should be changed to
<td><input asp-for="@Model.Members[i].Description" id="desc"/></td>
And this line
Description = addAttendanceVM.Description,
should be changed to
Description = att.Description,
You will also have to reinitialize your Members
because the type has changed
答案2
得分: 0
你有一次会议,参与者很多,并且你为每个参与者在数据库中存储一行数据,这会导致会议详情(比如Description
)的重复。
这被称为反规范化,通常应该避免使用(尽管在高级场景中可以用于改善读取性能)。
因此,一般建议至少使用两个表,一个用于会议,一个用于参加每次会议的参与者,尽管可能还需要一个表来存储每个参与者的详细信息。
总结一下,这些表应该如下所示:
Meetings
表,包含MeetingId
和其他字段,如Description
、Date
...Individuals
表,包含IndividualId
和其他详细信息,如Name
...Attendances
表,只包含MeetingId
和IndividualId
,它们应该是外键,指向前面的表。
请查看这篇关于在Entity Framework Core中实现这种关系的文章:https://learn.microsoft.com/en-us/ef/core/modeling/relationships/one-to-many
英文:
You have one meeting with many members, and you are storing one line in the database for each one, so it generates a duplication of the meeting details (like the Description
).
This is called denormalization, and it should generally be avoided (although it can be used in advanced scenarios to improve read performance).
So the general recommendation would be to use at least 2 tables, one for meetings and one for the attendees participating in each meeting, although probably you will also need a table for storing the details of each attendee.
To recap, the tables should look like this:
Meetings
table, withMeetingId
and other fields likeDescription
,Date
...Individuals
table, withIndividualId
and other details likeName
...Attendances
table, with onlyMeetingId
andIndividualId
, which should be Foreign Keys towards the previous tables.
Take a look at this article about implementing this kind of relationship in Entity Framework Core: https://learn.microsoft.com/en-us/ef/core/modeling/relationships/one-to-many
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论