C
C#14mo ago
Bamblobski

❔ save checkbox value after refresh

Is there a way in Blazor where I save the checkbox after I refresh the page, for example I press a checkbox and I refresh the page and the checkbox sould stayed clicked
No description
9 Replies
Iron
Iron14mo ago
You could save to browser or local storage , session storage would only save it for the current browser session which is what you are probably looking for
Bamblobski
BamblobskiOP14mo ago
not really, in my database i have a column with "IsPlannable", "IsPlannable" checks if its true or false, so even it i close the programm an i checked a Checkbox it should show me the enabled checkbox, but i dont know how ti implement that
No description
Iron
Iron14mo ago
Ohh , well do you have wasm with server or ? Because the way i do that in blazor is i make a call to my api to read or write from database using usermanager, if the value is stored in the users table If thats the case you would prob just read the values of the database when rendering the page with the checkboxes and set them to the values of the dB Im talking about if you have users logged in using identity tho might be a better way
Bamblobski
BamblobskiOP14mo ago
so about the user log, we use SSO, and i have a class with query about the "IsPlannable" yes ig
Iron
Iron14mo ago
Then yeah would prob be able to read or write from a controller
Bamblobski
BamblobskiOP14mo ago
Yes, but I'm not sure how to do it because I also use Telerik and I can't find any documentation for it or maybe i just need a method and but it into the GridBox so i wrote i method, but im not sure where to implement it, i tried in "GridCheckboxColumn" but that doesnt work
private void OnCheckboxChecked(object isChecked, CoreEmployee employee)
{
employee.IsPlannable = (bool)isChecked;
}
private void OnCheckboxChecked(object isChecked, CoreEmployee employee)
{
employee.IsPlannable = (bool)isChecked;
}
Bamblobski
BamblobskiOP14mo ago
if you need the hole code
Iron
Iron14mo ago
Not sure, first i would prob let telrik know the isPlannable property is bindable by adding a Bindable tag to it( if one still does that )


[Bindable(true)]
public bool IsPlannable { get; set; }


[Bindable(true)]
public bool IsPlannable { get; set; }
` That tells Telerik grid component to treat this property as bindable. Then it can be bound to a checkbox Im not so used to Telerik tho i might be wrong as i said. Then maybe use this:
<GridCheckboxColumn Field=@nameof(CoreEmployee.IsPlannable) CheckBoxOnlySelection="true" Title="Planbar" />
<GridCheckboxColumn Field=@nameof(CoreEmployee.IsPlannable) CheckBoxOnlySelection="true" Title="Planbar" />
` This will create a checkbox in the grid that is bound to the IsPlannable of each employee. Then modify the SaveData method to update the plannableEmployees list instead of updating allEmployees But as i said, if you have a DB, you prob need to make a API call to that DB to read/write, in my case, i usually create the database write/read stuff in my controllers, then i can access data or change using calls to the API. If you are using entity framework core you can do something like this:
`public class EmployeeService : IEmployeeService
{
private readonly ApplicationDbContext _dbContext;

public EmployeeService(ApplicationDbContext dbContext)
{
_dbContext = dbContext;
}

public async Task UpdateEmployee(CoreEmployee employee)
{
var dbEmployee = await _dbContext.CoreEmployees.FindAsync(employee.Id);
dbEmployee.IsPlannable = employee.IsPlannable;
await _dbContext.SaveChangesAsync();
}

public async Task UpdateEmployees(IEnumerable<CoreEmployee> employees)
{
foreach (var employee in employees)
{
var dbEmployee = await _dbContext.CoreEmployees.FindAsync(employee.Id);
dbEmployee.IsPlannable = employee.IsPlannable;
}

await _dbContext.SaveChangesAsync();
}
}
`public class EmployeeService : IEmployeeService
{
private readonly ApplicationDbContext _dbContext;

public EmployeeService(ApplicationDbContext dbContext)
{
_dbContext = dbContext;
}

public async Task UpdateEmployee(CoreEmployee employee)
{
var dbEmployee = await _dbContext.CoreEmployees.FindAsync(employee.Id);
dbEmployee.IsPlannable = employee.IsPlannable;
await _dbContext.SaveChangesAsync();
}

public async Task UpdateEmployees(IEnumerable<CoreEmployee> employees)
{
foreach (var employee in employees)
{
var dbEmployee = await _dbContext.CoreEmployees.FindAsync(employee.Id);
dbEmployee.IsPlannable = employee.IsPlannable;
}

await _dbContext.SaveChangesAsync();
}
}
` Dont listen to everything i say tho because i am not 100%, prob others in this discord who know way more about this. Im just giving you examples
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server