【问题标题】:View and Controller MVC 5视图和控制器 MVC 5
【发布时间】:2020-01-03 22:40:15
【问题描述】:

我的 GetCourseList.cshtml 文件(视图)中有以下代码,显示从数据库获取的信息:

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

当我运行项目时,我从这个视图中得到以下结果:

我希望用户使用这些复选框选择最喜欢的记录,然后当按下最终按钮(此按钮在我的图片中超出框架)时,此复选框的 id 将插入我的数据库(相关表)中。

认为我应该从我的 html 页面中获取选中的复选框 ID(但我不知道如何?!请帮助我!)并将此 ID 传递给操作,然后在我的操作中执行插入查询。

so:我应该将每个选中的复选框 id 传递给特定的控制器并收集它们(例如收集数组中的所有元素 id)有人告诉我我可以将我的视图元素映射到这个数组。但我没有给出这种方法的结果。这是问题! 请帮我找出我该怎么做。谢谢

【问题讨论】:

    标签: javascript html asp.net-mvc model-view-controller razor


    【解决方案1】:

    您可以在模型中添加属性 IsSelect

    Public bool IsSelect {get;set;}
    

    使用过

    @Html.CheckBoxFor(m => m.IsSelect)
    

    并在服务器中检查选择了哪一项

    var idList = items.Where(x=>x.IsSelect).Select(x=>x.Id).ToList();
    

    【讨论】:

      猜你喜欢
      • 2017-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多