Search code examples
htmlasp.net-mvc-4asp.net-mvc-views

MVC View (Dont want to reapeat the Id's)


I have a WCF service and and we had to generate a records of clients who have consumed free credit. I have generated that records but now clients with same login ID's are repeating in the records. I dnt want to display the login ID again but want to display all the numbers they have called. Here is the code of my view

         @model IEnumerable<MyYello.Admin.Models.CallHistory>

  @{ViewBag.Title = "UsersWhoHaveConsumedFreeCredit";
  Layout = "~/Views/Shared/_Layout.cshtml";
    }

  <h2>Users Who Have Consumed Free Credit</h2>
  <table class="table table-striped table-bordered tablesorter" style="display: block">
   <thead>
    <tr>
        <th>Login</th>
        <th>FirstName</th>
        <th>LastName</th>
        <th>Email</th>
        <th>Country</th>
        <th>Phone</th>
        <th>TarrifDesc</th>
        <th>CalledNum</th>
        <th>CallStart</th>
        <th>CallEnd</th>
    </tr>
    </thead>
    <tbody>
    @foreach (var item in Model)
    {
        <tr>
            <td>@item.Login</td>
            <td>@item.FirstName</td>
            <td>@item.LastName</td>
            <td>@item.Email</td>
            <td>@item.Country</td>
            <td>@item.Phone</td>
            <td>@item.TariffDescription</td>    
        </tr>    
    }
    @if (!Model.Any())
    {
        <tr>
            <td colspan="14">No Record Found</td>
        </tr>
    }
   </tbody>
 </table>

Just need an idea how


Solution

  • You should group by user login first then. Here's an example:

    @foreach (var group in Model.GroupBy(history => history.Login))
    {
        var item = group.First();
        <tr>
            <td>@item.Login</td>
            <td>@item.FirstName</td>
            <td>@item.LastName</td>
            <td>@item.Email</td>
            <td>@item.Country</td>
            <td>@string.Join(" - ", group.Select(history => history.Phone))</td>
            <td>@item.TariffDescription</td>
        </tr>
    }