Home > Back-end >  Blazor Server side - Modify model values from a control panel
Blazor Server side - Modify model values from a control panel

Time:03-30

I have a Blazor calculator that uses some inputs compares them to some system variables and shows the result.

I use a model with both Input variables and system variables:

(Example)

Input variables that i get and bind using:

<InputText style="width: 100%" @bind-Value="model.vrefavg" >
</InputText>

public double vrefavg { get; set; }

System variables then i compare with inputs:

if (model.vrefavg > model.dieselgasoilcf) {
model.Final=2000;
}

public double Dieselgasoilcf { get; set; } = 3.206;
public double lightfueloilcf { get; set; } = 3.151;
public double heavyfueloilcf { get; set; } = 3.114;

This System variables will need to change with the next few years, thus i want to be able to change them through a Login-> Admin Panel page i have created without having to edit the source code and reupload.

I have noticed that i can only @bind-value on the Index page, and i assume even if i work arround that, each time the application reloads the system variables will be set into their initial value that is on my model.

Any ideas on how tot do this?

Please note, first app ever.

CodePudding user response:

If you were to put something like this in your page:

protected override async Task OnInitializedAsync()
{
    var f = await File.ReadAllLinesAsync("fuels.txt");
    Dieselgasoilcf = double.Parse(f[0]);
    Lightfueloilcf = double.Parse(f[1]);
    Heavyfueloilcf = double.Parse(f[2]);
}

..then when the page is initing, the file would be read and the first 3 lines (the file would hence be 3.206\r\n3.151\r\n3.114) would be used to give values to the page variables

Presumably another page somewhere would set the values and write a new file..

The file is just an example; database would also be typical, but I wanted something quick and easy to demo the point - it's not about the file per se, it's about the notion that you store your data somewhere not baked into the code of the app, you read it in from that place, and assign it to your in-app variables, you use them, maybe you have some process that updates the file, the app quits, the app loads again tomorrow, the data is remembered..

That data can be anywhere sensible, outside the app; environment variables, an API call, a file, a database..

CodePudding user response:

You can use the project appsettings.json for such information. You can't update the values through a "Control Panel", but you make a simple change to the site configuration file. If you want some sort of control panel then you need to persist the data in a datastore (Database or File somewhere).

Here's the appsettings setup if you want to use it:

Add a section to appsettings.json

{
  "FuelCoefficients": {
    "Dieselgasoilcf": 3.206,
    "Lightfueloilcf": 3.151,
    "Heavyfueloilcf": 3.114
  },

  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}

Create a class to read the data into:

public class FuelCoefficients
{
    public const string SectionName = "FuelCoefficients";

    public double Dieselgasoilcf { get; set; }
    public double Lightfueloilcf { get; set; }
    public double Heavyfueloilcf { get; set; }
}

Add a section to Program to load the data

builder.Services.Configure<FuelCoefficients>(builder.Configuration.GetSection(FuelCoefficients.SectionName));

Use it wherever in your application through Dependancy Injection like this:

@page "/"
@using Microsoft.Extensions.Options
@inject IOptions<FuelCoefficients> fuelCoefficients

<PageTitle>Index</PageTitle>

<div>
    Diesel Value: @this.fuelCoefficients.Value.Dieselgasoilcf<br />
    Light Value: @this.fuelCoefficients.Value.Lightfueloilcf<br />
    Heavy Value: @this.fuelCoefficients.Value.Heavyfueloilcf<br />
</div>
  • Related