简体   繁体   English

如何将数据从复选框(html)导入数据库 ASP.NET Core MVC

[英]How to import data from checkbox (html) to database ASP.NET Core MVC

I have a list of guests which is stored in my database.我有一个客人列表,存储在我的数据库中。 After a party is created, I want the user to be able to invite guests to the party.创建聚会后,我希望用户能够邀请客人参加聚会。

To do that, I have two tables with a one-to-many relationship.为此,我有两个具有一对多关系的表。 In the first table (guests), I have the guests' information, and in the second table (PartyGuests), there are two other columns for referencing other stuff.在第一个表(客人)中,我有客人的信息,在第二个表(PartyGuests)中,还有另外两列用于引用其他内容。

After the user creates a party, a list of guests will appear in which the user can select whoever he wants to invite by selecting checkboxes.用户创建聚会后,将出现一个客人列表,用户可以在其中选择复选框来邀请他想要邀请的任何人。 Clicking on the Submit button passes all selected guests id to the controller.单击提交按钮将所有选定的客人 ID 传递给 controller。

Here is my View.这是我的观点。

            <table class="table">
                <thead class="thead-dark">
                    <tr>
                        <th scope="col">First</th>
                        <th scope="col">Last</th>
                        <th scope="col">Operation</th>
                    </tr>
                </thead>

                <tbody>

                    @foreach (var item in ViewBag.Guests)
                    {
                        <tr>
                            <td>@item.FirstName</td>
                            <td>@item.LastName</td>
                            
                            <td><input type="checkbox" name="guests" value="@item.Id"></td>
                        </tr>
                    }


                </tbody>

            </table>
            <input type="hidden" value="@Model.Id" name="eventId"/>
            <button type="submit" class="btn btn-primary">Invite</button>
        </form>

Here is my controller:这是我的 controller:

[HttpGet]
        public IActionResult Invite(int id)
        {
            ViewBag.Guests= _guestService.GetGuests(); //Display list of guests for invitation
            ViewBag.Id = id;

            return View(_eventService.GetEvent(id));    // Return event to get the event information o the Invite page
        }

        [HttpPost]
        public IActionResult Invite(int eventId, string[] guests)
        {
            foreach (var item in guests)
            {
                var newGuest = new EventGuest
                {
                    EventId = eventId,
                    GuestId = int.Parse(item)
                };

                // LINQ -- To avoid invite a guest more than once
                if (_eventService.IsInvited(newGuest.GuestId, eventId))  // Does not invite
                {
                    return RedirectToAction("Error", "Something");  // Give an error message
                }
                else
                {
                    _eventService.InviteGuest(newGuest);
                    _eventService.SaveChanges();
                }
            }
            return View("Index", "Home");
        }

Now, the problem is that I want the invited guest checkboxes to be checked when they already invited.现在,问题是我希望在他们已经被邀请时检查被邀请的客人复选框。 In another word, if the party manager wants to add more, he can be able to recognize the invited guests by looking at the checked checkboxes.换句话说,如果派对经理想要添加更多,他可以通过查看选中的复选框来识别被邀请的客人。

I should note that my database get valid data in its columns through this operation我应该注意,我的数据库通过此操作在其列中获取有效数据

I am not familiar with JavaScript, so please help me with HTML.我不熟悉 JavaScript,所以请帮助我了解 HTML。

Thank you谢谢

The first thing to note is that you have a string array for guests in the action but I suspect this should be an array integers.首先要注意的是,您在操作中有一个用于客人的字符串数组,但我怀疑这应该是一个整数数组。 You could also have a generic list of integers which should also work.您还可以有一个通用的整数列表,它也应该可以工作。

The following worked for me - note that if no checkboxes are checked then the list of ID's is empty以下内容对我有用 - 请注意,如果未选中任何复选框,则 ID 列表为空

<form method="post" asp-area="YourArea" asp-controller="YourController" asp-action="Test">
    <button type="submit">Post</button>
    <input type="hidden" value="123" name="eventId" />
    @for (int index = 0; index < Model.Count; index++)
    {
        <input type="checkbox" name="checkedIdList" value="@Model[index].Id" />
    }
</form>
        
            
[HttpPost]
public async Task<IActionResult> Test(int eventId, List<int> checkedIdList)
{
    await Task.CompletedTask;
            
    return RedirectToAction("Index");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从数据库中获取 ASP.NET MVC 中选中的复选框? - How to get checkbox selected in ASP.NET MVC from database? 使用ASP.NET MVC Core将数据从数据库注入到类中 - Inject data from database to a class with ASP.NET MVC Core 如何从统计类型数据的数据库即时获取枚举中的数据? 在asp.net核心mvc中 - How to get data in enum from database instant of stastically typed data? in asp.net core mvc 如何在 ASP.NET Core MVC 中显示数据库中的图像? - How to display an image from the database in ASP.NET Core MVC? 我如何引用MVC项目中的类,该类使用asp.net核心从数据库中检索数据 - How can I reference a class in MVC project that retrieves data from database with asp.net core 如何从 ASP.NET Core MVC 中的数据库中获取相关数据 - How to get related data from database in ASP.NET Core MVC 如何使用ASP.net MVC从数据库中获取数据? - How to fetch data from database using ASP.net MVC? 如何将数据库中的数据添加到 ASP.NET Core MVC (.NET 6) 应用程序中的共享 _Layout.cshtml? - How do you add data from the database to the shared _Layout.cshtml in ASP.NET Core MVC (.NET 6) application? 如何将数据库逻辑从我的 Asp.Net MVC 应用程序转移到 ASP.Net Core MVC? - How can I transfer the database logic from my Asp.Net MVC Apllication to ASP.Net Core MVC? HTML “复选框” 与 asp.net 核心 MVC 连接,将 model 连接到复选框按钮 - HTML “checkbox” connect with asp.net core MVC , connecting model to checkbox buttons
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM