Home > Software design >  The name 'Ok' does not exist in the current context, Razor Code behind
The name 'Ok' does not exist in the current context, Razor Code behind

Time:01-25

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

CodePudding user response:

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());
}

CodePudding user response:

Razor Pages != Controllers, hydrate a model property then return Page() instead:

public IEnumerable<Customer>  Customers {get;set;}

public async Task<IActionResult> OnGetDetails(string custName)
{
    Customers = await _context.Customer...
    
    return Page();
}

Then use customers in your Page with Model.Customers...

  • Related