skip to Main Content

In my code behind I have the following and I’m getting the error ‘Ok’ does not exist in current context. I’m trying to populate a form with textboxes.

public async Task<IActionResult> OnGetDetails(string custName)
{
    var x = _context.Customer
            .Join(_context.Sales, 
            x => x.CustId,
            c => c.CustId,
            (x,c) => new
            { 
                customerName = x.Name,
                address = x.Address,
                sale = x.SaleDate
            }).ToListArraySync();

         return Ok(await q.ToListArraySync()); //causes error 'Ok' doesn't exist in current context

}

I need to prefill the form on the page out with the data. I’m able to get the data on the default OnGet(), however, I need to join two tables in this handler

2

Answers


  1. Chosen as BEST ANSWER

    I got it working.

    I added a FK to the table and rebuilt the models and then this:

     public async Task<IActionResult> OnGetDetails(string custName)
    {
    
            var x = _context.Customer
                    .Join(_context.Sales, 
                    x => x.CustId,
                    c => c.CustId,
                    (x,c) => new
                    { 
                        customerName = x.Name,
                        address = x.Address,
                        sale = x.SaleDate
                    }).ToListArraySync();
        
                 return Ok(await q.ToListArraySync()); 
        
        }
    

    became this:

    public async Task<IActionResult> OnGetDetails(string custName)
    {
        var x = _context.Customer
            .Where(c => c.CustomerName == custName)
            .Include(x => x.Sales).ToListArraySync()
    }
    

    and now I can see the data from both tables on my view


  2. You can "manually" return OkObjectResult or JsonResult. ControllerBase.Ok is just a convenience method that actually returns an OkObjectResult

    public async Task<IActionResult> OnGetDetails(string custName)
    {
        // ...
    
        return new OkObjectResult(await q.ToListArraySync());
    
        // or
        return new JsonResult(await q.ToListArraySync());
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search