ASP.NET Core MVC : GET details using view model not working

Viewed 27

I am having a problem using a view model to view get the details of an item selected in the view. I am loading a result into my index view as follows

public async Task<IActionResult> Index(string sortOrder, string currentFilter, string searchString, int? page)
{
    ViewBag.CurrentSort = sortOrder;
    ViewBag.ConOrgSortParm = String.IsNullOrEmpty(sortOrder) ? "Client_desc" : "Client";
    ViewBag.AssignedSortParm = String.IsNullOrEmpty(sortOrder) ? "Assigned_desc" : "Assigned";
    ViewBag.ExpiresSortParm = String.IsNullOrEmpty(sortOrder) ? "Expires_desc" : "Expires";
    ViewBag.LastActiveSortParm = String.IsNullOrEmpty(sortOrder) ? "LastActive_desc" : "LastActive";
    ViewBag.IDSortParm = String.IsNullOrEmpty(sortOrder) ? "Id_desc" : "";

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

    ViewBag.CurrentFilter = searchString;

    var results = (from node in _context.Nodes
                   join bnbridge in _context.BundleNodes on node.Id equals bnbridge.NodeId into NodeBundleIDGroup
                   from ax in NodeBundleIDGroup.DefaultIfEmpty()
                   join bundle in _context.Bundles on ax.BundleId equals bundle.Id into NodeBundleGroup
                   from bx in NodeBundleGroup.DefaultIfEmpty()
                   join agreement in _context.Agreements on bx.AgreementId equals agreement.Id into agGroup
                   from cx in agGroup.DefaultIfEmpty()
                   join org in _context.Organizations on cx.OrgId equals org.Id into oGroup
                   from ex in oGroup.DefaultIfEmpty()
                   join conorg in _context.Organizations on node.OrgId equals conorg.Id into tGroup
                   from fx in tGroup.DefaultIfEmpty()
                   select new NodeIndexViewModel
                     {
                         Id = node.Id,
                         Name = node.Name,
                         AssignedOrg = fx.ShortName,
                         ContractingOrg = ex.ShortName,
                         Expiry = bx.EndUtc,
                         LastActive = node.ActiveDate,
                         NodeType = bx.NodeTypeID
                     });

    if (!String.IsNullOrEmpty(searchString))
    {
        results = results.Where(s => s.AssignedOrg.Contains(searchString)
                                        || s.Id.ToString().StartsWith(searchString));
    }

    switch (sortOrder)
    {
        case "Client_desc":
            results = results.OrderByDescending(s => s.ContractingOrg);
            break;

        case "Client":
            results = results.OrderBy(s => s.ContractingOrg);
            break;

        case "Assigned":
            results = results.OrderBy(s => s.AssignedOrg);
            break;

        case "Assigned_desc":
            results = results.OrderByDescending(s => s.AssignedOrg);
            break;

        case "Expires":
            results = results.OrderBy(s => s.Expiry);
            break;

        case "Expires_desc":
            results = results.OrderByDescending(s => s.Expiry);
            break;

        case "LastActive":
            results = results.OrderBy(s => s.LastActive);
            break;

        case "LastActive_desc":
            results = results.OrderByDescending(s => s.LastActive);
            break;

        case "Id_desc":
            results = results.OrderByDescending(s => s.Id);
            break;

        default:
            results = results.OrderBy(s => s.Id);
            break;
    };

    int pageSize = 10;
    int pageNumber = (page ?? 1);

    return View(await results.ToPagedListAsync(pageNumber, pageSize));
}

This is working as expected. The problem comes when I want to access the Details of one of the listed results.

I have tried the following in the controller:

public async Task<IActionResult> Details(int? id)
{
    if (id == null || _context.NodeIndexViewModel == null)
    {
        return NotFound();
    }

    var nodeIndexViewModel = await _context.NodeIndexViewModel
                                           .FirstOrDefaultAsync(m => m.Id == id);

    if (nodeIndexViewModel == null)
    {
        return NotFound();
    }

    return View(nodeIndexViewModel);
}

I get an error

PostgresException: 42P01: relation "NodeIndexViewModel" does not exist

1 Answers

Can't answer the question because I don't know what the problem was but I rescaffolded the database and it's working now.

Related