保存多行HTML表格到数据库中的方法

huangapple go评论73阅读模式
英文:

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

保存多行HTML表格到数据库中的方法

这是数据库中的结果
保存多行HTML表格到数据库中的方法

感谢您的帮助。谢谢。

英文:

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&lt;SelectListItem&gt; Members { get; set; }
public string MemberId { get; set; }

View

 @using (Html.BeginForm(&quot;AddAttendance&quot;,&quot;Attendance&quot;,FormMethod.Post))
{
&lt;div class=&quot;card-body&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;input type=&quot;date&quot; asp-for=&quot;@Model.Date&quot; class=&quot;form-control js-datepicker&quot; asp-format=&quot;{0:dd/MMM/yyyy}&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;select type=&quot;text&quot; id=&quot;meetingType&quot; class=&quot;select2 form-control&quot; asp-items=&quot;@Model.Meetings&quot; asp-for=&quot;@Model.MeetingId&quot; style=&quot;width:100%&quot; multiple&gt;&lt;/select&gt;
&lt;/div&gt;
&lt;div class=&quot;table-responsive&quot;&gt;
&lt;table class=&quot;table table-bordered&quot; id=&quot;dataTable&quot; width=&quot;100%&quot; cellspacing=&quot;0&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;th&gt;Action&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
@for (int i = 0; i &lt; Model.Members.Count(); i++)
{
&lt;tr&gt;
&lt;td&gt;@Model.Members[i].Text&lt;/td&gt;
&lt;td&gt;&lt;input asp-for=&quot;@Model.Description&quot;  id=&quot;desc&quot;/&gt;&lt;/td&gt;
&lt;td&gt;
&lt;input type=&quot;hidden&quot; class=&quot;custom-checkbox&quot; asp-for=&quot;@Model.Members[i].Value&quot; /&gt;
&lt;label class=&quot;a8z-toggle-switch&quot; data-size=&quot;sm&quot; data-color=&quot;blue&quot; data-text=&quot;true&quot; data-style=&quot;rounded&quot;&gt;
&lt;input asp-for=&quot;Members[i].Selected&quot; type=&quot;checkbox&quot; id=&quot;example_default_1&quot;&gt;
&lt;span class=&quot;toggle&quot;&gt;
&lt;span class=&quot;switch&quot;&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/label&gt;
&lt;/td&gt;
&lt;/tr&gt;
}
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;

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 =&gt; c.Type == &quot;Name&quot;).Value,
CreatedDate = DateTime.Now
};
xct.Attendances.Add(addThisAttendance); 
}
xct.SaveChanges();
return ViewComponent(nameof(AttendanceLists));

保存多行HTML表格到数据库中的方法

and this is the results in the database
保存多行HTML表格到数据库中的方法

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=&quot;Model.Description&quot; 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&lt;Member&gt; Members { get; set; }

After this, this line

&lt;td&gt;&lt;input asp-for=&quot;@Model.Description&quot;  id=&quot;desc&quot;/&gt;&lt;/td&gt;

Should be changed to

&lt;td&gt;&lt;input asp-for=&quot;@Model.Members[i].Description&quot;  id=&quot;desc&quot;/&gt;&lt;/td&gt;

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)的重复。

这被称为反规范化,通常应该避免使用(尽管在高级场景中可以用于改善读取性能)。

因此,一般建议至少使用两个表,一个用于会议,一个用于参加每次会议的参与者,尽管可能还需要一个表来存储每个参与者的详细信息。

总结一下,这些表应该如下所示:

  1. Meetings 表,包含 MeetingId 和其他字段,如 DescriptionDate...
  2. Individuals 表,包含 IndividualId 和其他详细信息,如 Name...
  3. Attendances 表,只包含 MeetingIdIndividualId,它们应该是外键,指向前面的表。

请查看这篇关于在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:

  1. Meetings table, with MeetingId and other fields like Description, Date...
  2. Individuals table, with IndividualId and other details like Name...
  3. Attendances table, with only MeetingId and IndividualId, 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

huangapple
  • 本文由 发表于 2023年7月20日 16:45:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定