I have an application that shows a list of errors. I use this line of code to group each error by its application name on the index page var applications = db.ElmahErrors.GroupBy(s => s.Application).Select(grp => grp.FirstOrDefault());

This then produces this output. enter image description here

As you can see from the image, each application name is a hyperlink, once clicked, I want it to take you to another page and display only the errors that also have that application name. For example, if you clicked the top result /LM/W3SVC/7/ROOT. There are 13 instances that have the same application name that I wish to show.

How it currently stands is that when I click the hyperlink, it brings up every single error in the database and not just the ones with a related application name like so.

enter image description here

The output that I would actually like to see is this:

enter image description here

Controller

public ActionResult Index(string sortOrder, string searchString, string currentFilter, int? page)
{
    ViewBag.CurrentSort = sortOrder;
    ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";

    if (searchString != null)
    {
        page = 1;
    }
    else
    {
        searchString = currentFilter;
    }

    ViewBag.CurrentFilter = searchString;

    var applications = db.ElmahErrors.GroupBy(s => s.Application).Select(grp => grp.FirstOrDefault());

    if (!String.IsNullOrEmpty(searchString))
    {
        applications = applications.Where(s => s.Application.Contains(searchString));
    }

    switch (sortOrder)
    {
        default:
            applications = applications.OrderBy(x => x.Application).ThenByDescending(s => s.TimeUtc);
            break;
    }

    int pageSize = Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["DefaultPageSize"]);
    int pageNumber = (page ?? 1);
    return View(applications.ToPagedList(pageNumber, pageSize));
    }

//
// GET: /Application/Errors/5

public ActionResult Errors(string sortOrder, string searchString, string currentFilter, int? page)
{
    ViewBag.CurrentSort = sortOrder;
    ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";

    if (searchString != null)
    {
        page = 1;
    }
    else
    {
        searchString = currentFilter;
    }

    ViewBag.CurrentFilter = searchString;

    var applications = from s in db.ElmahErrors 
                           select s;

    if (!String.IsNullOrEmpty(searchString))
    {
        applications = applications.Where(s => s.Application.Contains(searchString));
    }

    switch (sortOrder)
    {
        default:
            applications = applications.OrderBy(x => x.Application).ThenByDescending(s => s.TimeUtc);
                break;
    }

    int pageSize = Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["DefaultPageSize"]);
    int pageNumber = (page ?? 1);
    return View(applications.ToPagedList(pageNumber, pageSize));
}

Index View

@model PagedList.IPagedList<DataIntelligence.Models.ElmahError>
@using PagedList.Mvc;
<link href="Content/PagedList.css" rel="stylesheet" type="text/css" />

@{
    ViewBag.Title = "Application Error Dashboard";
}

<div class="jumbotron">
    <h2>Application Error Dashboard</h2>
</div>

<table>
    <tr>
        <th>
            Application
        </th>
        <th>
            Host
        </th>
        <th>
            Type
        </th>
        <th>
            Date
        </th>
        <th>
            Message
        </th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                <a href="@Url.Action("Errors", new { id=item.ErrorId})">> @Html.DisplayFor(modelItem => item.Application) </a>
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Host)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Type)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.TimeUtc)
            </td>
            <td>
                @{
        string parameterValue = item.Message.Substring(0, 12);
                }

                @Html.DisplayFor(modelItem => parameterValue)
            </td>
        </tr>
    }
</table>
<br />
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount

@Html.PagedListPager(Model, page => Url.Action("Index", new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))

Related Error View

@model PagedList.IPagedList<DataIntelligence.Models.ElmahError>
@using PagedList.Mvc;
<link href="Content/PagedList.css" rel="stylesheet" type="text/css" />

@{
    ViewBag.Title = "Application Error Dashboard";
}

<div class="jumbotron">
    <h2>Application Error Dashboard</h2>
</div>

<table>
    <tr>
        <th>
            Id
        </th>
        <th>
            Application
        </th>
        <th>
            Host
        </th>
        <th>
            Type
        </th>
        <th>
            Date
        </th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                <a href="@Url.Action("Details", new { id=item.ErrorId})"> @Html.DisplayFor(modelItem => item.ErrorId) </a>
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Application)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Host)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Type)
            </td>
            <td>

                @Html.DisplayFor(modelItem => item.TimeUtc)
            </td>
        </tr>
    }
</table>
<br />
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount

@Html.PagedListPager(Model, page => Url.Action("Errors", new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))

Any help on this would be greatly appreciated as I just can't seem to get my head round it.

Thanks in advance.

Related posts

Recent Viewed