[英]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.