过帐项目列表的复选框的值 [英] Posting the value of the checkbox for a list of items
问题描述
我仍然在学习asp.net核心方面还很陌生,并且我面临复选框的问题.我有一个项目(课程)的列表,这些项目是通过存储在sql表中的方式显示的,并且正在视图中的表中显示它们,我为复选框添加了以下代码,但未发布任何值.我已经尝试了很多复杂的解决方案,但是在我的情况下它们都不起作用,因此我们将不胜感激.
I'm still new in learning asp.net core, and I'm facing this problem with checkboxes. I have a list of items (courses), which are by the way stored in a sql table, and I'm displaying them in a table inside the view, I added the below piece of code for the checkbox but it's not posting any value. I've tried a lot of complicated solutions but none of them worked in my case, so any help would be highly appreciated.
- 视图:
@foreach (Course course in Model)
{
<tr>
<td>@Html.CheckBoxFor(modelItem => course.isSelected)</td>
</tr>
}
- 模型(课程)具有以下属性:
[DefaultValue(false)]
public bool isSelected { get; set; }
- 控制器:
public async Task<ActionResult> Index(string searchString)
{
var courses = from m in _context.Courses
select m;
ViewData["Filtering"] = searchString;
if (searchString != null)
{
courses = courses.Where(m => m.Name.Contains(searchString.ToUpper().Replace(" ", "")));
}
return View(await courses.AsNoTracking().ToListAsync());
}
推荐答案
如果要将复选框值列表发布到 HttpPost
操作,则可以使用设置相同的名称
每个复选框的属性,请参阅我的演示:
If you want to post list of checkbox value to HttpPost
action, you could use set the same name
attribute for each checkbox, refer to my demo:
1.View(使用表单标签)
1.View(use a form tag)
<form method="post">
@foreach (Course course in Model)
{
<tr>
<td> <input type="checkbox" name="courseSelectedStatus" value="@course.isSelected" />
</tr>
}
<input type="submit" value="submit" />
</form>
2.动作:
[HttpPost]
public async Task<IActionResult> Index(List<bool> courseSelectedStatus)
{
//other logic
return View();
}
由于不清楚如何处理复选框值,您还可以参考下面的链接,该链接在List模型中传递数据:
Since it is not clear how do you handle the checkbox value,you could also refer to below link which pass the data in List model:
更新(7/24/2019):
Update(7/24/2019):
复选框的值是否可以存储in(课程的ID),因此在控制器中,我可以遍历每个int i并更改具有id的课程的"isSelected"的值=我,成真的吗?(只要复选框在选中时就可以存储一个int值)
Is it possible for the value of the checkbox to store an in (the id of the course), so in the controller, I can loop over each int i and change the value of "isSelected", for the course having id = i, into true? (As long the checkbox can store an int value whenever it is checked)
是的,您可以将复选框值设置为Id并在控制器中作为列表接收,请参阅我的以下代码:
Yes, you could set checkbox value as Id and receive as List in the controller,refer to my below code:
1.视图:
<form method="post">
@foreach (Course course in Model)
{
<tr>
<td> <input type="checkbox" name="courseSelectedStatus" value="@course.Id" />
</tr>
}
<input type="submit" value="submit" />
</form>
2.动作:
[HttpPost]
public async Task<IActionResult> Index(List<int> courseSelectedStatus)
{
foreach(var i in courseSelectedStatus)
{
var employee = await _context.Courses.Where(e => e.Id == i).FirstOrDefaultAsync();
employee.isSelected = true;
_context.Update(employee);
await _context.SaveChangesAsync();
}
//other logic
}
这篇关于过帐项目列表的复选框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!