Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Count number of items in model using IEnumerable<T>

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>
like image 245
Matchbox2093 Avatar asked Mar 24 '15 10:03

Matchbox2093


1 Answers

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

like image 132
Matchbox2093 Avatar answered Sep 30 '22 05:09

Matchbox2093