簡體   English   中英

如何將數據從復選框(html)導入數據庫 ASP.NET Core MVC

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

我有一個客人列表,存儲在我的數據庫中。 創建聚會后,我希望用戶能夠邀請客人參加聚會。

為此,我有兩個具有一對多關系的表。 在第一個表(客人)中,我有客人的信息,在第二個表(PartyGuests)中,還有另外兩列用於引用其他內容。

用戶創建聚會后,將出現一個客人列表,用戶可以在其中選擇復選框來邀請他想要邀請的任何人。 單擊提交按鈕將所有選定的客人 ID 傳遞給 controller。

這是我的觀點。

            <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>

這是我的 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");
        }

現在,問題是我希望在他們已經被邀請時檢查被邀請的客人復選框。 換句話說,如果派對經理想要添加更多,他可以通過查看選中的復選框來識別被邀請的客人。

我應該注意,我的數據庫通過此操作在其列中獲取有效數據

我不熟悉 JavaScript,所以請幫助我了解 HTML。

謝謝

首先要注意的是,您在操作中有一個用於客人的字符串數組,但我懷疑這應該是一個整數數組。 您還可以有一個通用的整數列表,它也應該可以工作。

以下內容對我有用 - 請注意,如果未選中任何復選框,則 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM