Javascript 视图和控制器MVC5
我的GetCourseList.cshtml文件(视图)中有以下代码,显示从数据库获取的信息:Javascript 视图和控制器MVC5,javascript,html,asp.net-mvc,model-view-controller,razor,Javascript,Html,Asp.net Mvc,Model View Controller,Razor,我的GetCourseList.cshtml文件(视图)中有以下代码,显示从数据库获取的信息: @model IEnumerable<WebApplication8.Models.Courses> @{ Layout = null; } <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com
@model IEnumerable<WebApplication8.Models.Courses>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<meta name="viewport" content="width=device-width" />
<title>GetCourseList</title>
<style>
.table thead th,
.table tbody td {
text-align: center;
}
</style>
</head>
<body>
<table class="table table-striped">
<thead>
<tr>
<th scope="col" class="border-top-0 text-center">course name</th>
<th scope="col" class="border-top-0">unit</th>
<th scope="col" class="border-top-0">term</th>
<th scope="col" class="border-top-0">choose</th>
<th scope="col" class="border-top-0"></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td class="align-middle">@item.name</td>
<td class="align-middle">@item.unit</td>
@if (item.term == 0)
{
<td class="align-middle">custom</td>
<td>
<input class="btn btn-success" type="checkbox" value=@item.name id=@item.course_id/>
</td>
}
else
{
<td class="align-middle">@item.term</td>
<td><input type="checkbox" value=@item.name id=@item.course_id /></td>
}
<td class="text-right align-middle">
@*<button type="button" class="btn btn-success">choose</button>*@
</td>
</tr>
}
</tbody>
</table>
</body>
</html>
@model IEnumerable
@{
布局=空;
}
客串
.表thead th,
.表tbody td{
文本对齐:居中;
}
课程名称
单元
学期
选择
@foreach(模型中的var项目)
{
@item.name
@项目.单位
@如果(item.term==0)
{
习俗
}
其他的
{
@项目.期限
}
@*选择*@
}
当我运行该项目时,我从该视图中得到以下结果:
我希望用户使用这些复选框选择最喜爱的记录,然后当按下最后一个按钮(该按钮在我的图片中超出了框架)时,将该复选框的id插入我的数据库(相关表)
我想我应该从我的html页面中选中复选框id(但我不知道怎么做?!请帮助我!),并将此id传递给操作,然后在我的操作中执行插入查询
因此:我应该将每个复选框id传递给特定的控制器并收集它们(例如收集数组中的所有元素id)。有人告诉我,我可以将视图元素映射到此数组。但我不会给出这种方法的结果。这就是问题!
请帮我找出我该怎么做。谢谢您可以在模型中添加属性IsSelect
Public bool IsSelect {get;set;}
使用
@Html.CheckBoxFor(m => m.IsSelect)
并在服务器中检查选择了哪一项
var idList = items.Where(x=>x.IsSelect).Select(x=>x.Id).ToList();