如何使用IEnumerable <T>计算模型中的项目数

Mat*_*093 5 c# model-view-controller ienumerable model

我想知道是否有可能为模型中的项目数量做一个计数器,我知道使用list是可能的,但id更喜欢使用IEnumerable用于项目目的.如果它可以在javascript中进行持续更新,但我在MVC和Javascript中没有足够的经验,那会更好.

-Controller-

public class ModelController : Controller
{
    private JobData db = new JobData();

    //
    // GET: /Model/

    public ActionResult Index()
    {
        var Model = db.Models.OrderByDescending(model => model.ModelType);
        int count = Model.Count();

        return View(Model.ToList());
    }
Run Code Online (Sandbox Code Playgroud)

-视图-

    @model IEnumerable<JobTracker.Models.Model>

@{
    ViewBag.Title = "Camera Models";
}

 <script type="text/javascript" src="~/Scripts/Table.js"></script>




<table id="tfhover" class="tftable" border="1">  
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.ModelType)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {


    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.ModelType)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ModelID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ModelID }) 
          @*  @Html.ActionLink("Delete", "Delete", new { id=item.ModelID })*@
        </td>
    </tr>
}

</table>
Run Code Online (Sandbox Code Playgroud)

Mat*_*093 15

您可以在@Model.Count()任何地方使用显示计数 - (从评论部分获取@MukeshModhvadiya)