0

I have dependency ICollectionService:

public class CollectionService : ICollectionService
{
    public async Task<bool> IsObtained(int gameId)
    {
        using (DatabaseContext context = _contextFactory.CreateDbContext())
        {
            List<CollectionGame> collectionGame = await context.Set<CollectionGame>().Where<CollectionGame>(cg => cg.GameId.Equals(gameId)).ToListAsync();
            return collectionGame.FirstOrDefault<CollectionGame>().Obtained;
        }
    }
}

The object [Obtained] is a bool in the backend.

I have injected ICollectionService in page Games.razor:

@inject ICollectionService ICollectionService

// ...

<tbody>
    @foreach (var game in games)
    {
        <tr>
            <td>@game.Upc</td>
            <td>@game.Title</td>
            <td class="center"><input type="checkbox" @bind="ICollectionService.IsObtained(game.Id)"></td>
            <td>@game.Paid</td>
        </tr>
    }
</tbody>

// ...

game.Id is an int.

I am receiving the following error caused by line <td class="center"><input type="checkbox" @bind="ICollectionService.IsObtained(game.Id)"></td>

Severity    Code    Description Project File    Line    Suppression State
Error   CS0131  The left-hand side of an assignment must be a variable, property or indexer ProjectName ...\SolutionName\ProjectName\Microsoft.NET.Sdk.Razor.SourceGenerators\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Pages_Games_razor.g.cs  215 Active

I have checked my code and I am unsure why this error is being thrown. May you please advise?

TIA.

1 Answers1

0

In my opinion,@bind cannot assign an expression, it can only be a definite value.You can assign the boolean value obtained by calling the service to a variable and then refer to the variable.

Below is my test code,it works fine:

CollectionService:

public class CollectionService : ICollectionService
{
    public bool IsObtained(int gameId)
    {
        return false;
    }
}

Games.razor:

@inject ICollectionService collectionService
@{
    bool ticked = collectionService.IsObtained(0);
}
<table class="table">
    <tr>
        <td>Test</td>
        <td>Test</td>
        <td><input type="checkbox" @bind = "ticked" /></td>
    </tr>
</table>

Test Result:

enter image description here enter image description here

Chen
  • 4,499
  • 1
  • 2
  • 9