如何使用IEnumerable 对模型中的项目数进行计数(How to Count number of items in model using IEnumerable)

我想知道是否有可能做一个模型中的项目数量计数器,我知道使用列表是可能的,但编号喜欢使用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()); }

-视图-

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

I was wondering is it possible to do a counter for the number of items in a model, i know using list it is possible, but id prefer to use IEnumerable for the project purpose. it would be even better if it could be done in javascript for constant update but im not experienced enough in MVC and 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()); }

-View-

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

最满意答案

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

You can just use @Model.Count() to display count anywhere – (credit goes to @MukeshModhvadiya from the comment section)

如何使用IEnumerable 对模型中的项目数进行计数(How to Count number of items in model using IEnumerable)

我想知道是否有可能做一个模型中的项目数量计数器,我知道使用列表是可能的,但编号喜欢使用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()); }

-视图-

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

I was wondering is it possible to do a counter for the number of items in a model, i know using list it is possible, but id prefer to use IEnumerable for the project purpose. it would be even better if it could be done in javascript for constant update but im not experienced enough in MVC and 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()); }

-View-

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

最满意答案

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

You can just use @Model.Count() to display count anywhere – (credit goes to @MukeshModhvadiya from the comment section)