我如何在声明@model IENumerable的视图页面上使用@ Html.helper

时间:2018-11-29 08:12:41

标签: c# asp.net-mvc bootstrap-4

我的视图页面正在使用@model IENumerable,因为我在代码中包含了@foreach(模型中的var项)。现在我无法在页面上使用@ Html.textboxfor等。 我该如何在我的视图页面上使用@ Html.helper或通过使用@model模型而不是@model IENumerable在我的视图页面上进行循环播放?

我的查看代码如下:

@model IEnumerable<OnlinePlatform.Models.Services>
<script>

$(document).ready(function () {

    $(document).on("click", '.edit_button', function (e) {
        var serviceid = $(this).data('id');
        var name = $(this).data('name');
        var desc = $(this).data('description');
        var internet = $(this).data('interneturl');
        var intranet = $(this).data('intraneturl');
        var systemowner = $(this).data('systemowner');
        var iconpath = $(this).data('iconpath');

        $(".editService_ServiceId").val(serviceid);
        $(".editService_Name").text(name);
        $(".editService_Description").val(desc);
        $(".editService_InternetURL").val(internet);
        $(".editService_IntranetURL").val(intranet);
        $(".editService_SystemOwner").val(systemowner);
        $(".editService_IconPath").val(iconpath);

    });

    $(document).on("click", '.request_button', function (e) {
        var serviceid = $(this).data('id');
        var servicename = $(this).data('name')

        $(".requestService_Name").text(servicename);
        $(".requestService_ServiceId").val(serviceid);
    });

});

<section class="section">
<div class="row">
    <div class="col-md-12">
        <h2>Please choose a service to start</h2>
        <br />
        <div class="row">


            <div class="col-3">
                <div class="card text-center">
                    <div style="min-height:113px">
                        <a href="#addServiceModal" data-toggle="modal"><i class="fa fa-plus pt-4" style="font-size:76px"> </i></a>
                    </div>
                    <div class="card-body">
                        <h4 class="card-title" style="height:85px">New Service?</h4>
                        <p class="card-text" style="height:47px">
                            Click here to add a new service

                        </p>
                        <a href="#" class="btn btn-info" data-toggle="modal" data-target="#addServiceModal">Add a Service</a>
                    </div>
                </div>
            </div>

            @foreach (var item in Model)
            {

                <div class="col-3">
                    <div class="card text-center">
                        <a href="@item.IntranetUrl">
                            <img class="text-center" src="~/Images/Services/corporate-calendar.png">
                        </a>
                        <div class="card-body">
                            <h4 class="card-title" style="height:85px">@item.Name</h4>
                            <p class="card-text" style="height:47px">
                                @item.Description

                            </p>
                            <a href="@item.IntranetUrl" class="btn btn-success">Access</a>
                            <a href="#editServiceModal" class="btn btn-info edit_button" data-toggle="modal"
                               data-id="@item.Id"
                               data-name="@item.Name"
                               data-description="@item.Description"
                               data-interneturl="@item.InternetUrl"
                               data-intraneturl="@item.IntranetUrl"
                               data-systemowner="@item.SystemOwner"
                               data-iconpath="@item.IconPath">Edit</a>
                            <a href="#requestServiceModal" class="btn btn-danger request_button" data-toggle="modal"
                               data-id="@item.Id"
                               data-name="@item.Name">Register</a>
                        </div>
                    </div>
                </div>
            }


            <div class="container">
                <h2>Striped Rows</h2>
                <p>The .table-striped class adds zebra-stripes to a table:</p>
                <table class="table">
                    <thead>
                        <tr>
                            <th>Id</th>
                            <th>Name</th>
                            <th>Description</th>
                            <th>Start Date</th>
                            <th>System Owner</th>
                            <th>Created By</th>
                            <th></th>
                        </tr>
                    </thead>

                    @foreach (var item in Model)
                    {
                        <tbody>
                            <tr>
                                <td>@item.Id</td>
                                <td>@item.Name</td>
                                <td>@item.Description</td>
                                <td>@item.StartDate</td>
                                <td>@item.SystemOwner</td>
                                <td>@item.CreatedBy</td>
                                <td><button type="button" class="btn">Edit</button></td>
                            </tr>
                        </tbody>
                    }


                </table>
            </div>



        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

这是您通常循环播放的方式,因为foreach不提供索引:

@for(int i=0; i<Model.Count; i++)
{
    @Html.TextBoxFor(m => m[i].Property)
}