A simple library to generate options page for websites
In every website, we need an options page (or setting page) where users can change some configs inside. Such as the number of rows in the lists, allowing comments on posts or not, changing themes and any other things.
To have any of those inputs in your options page, you should create a row in your table, create an input in the front-end, write the logic for that in the back-end and inserting its value in DB when every user clicks on the save button.
With AutoOption you can create any input inside your options page in a couple of seconds
Install AutoOption and AutoOption.Pages from NuGet to your project
(If you have Database and ConnectionString, skip the step 1 and 2)
-
Create a Database
-
Create a ConnectionString
Create a ConnectionString the way you want or like my ConnectionString in appsettings.json (like here) -
Options Class
Create an empty class withOptions
name wherever you want -
Options Page
-
Right click on
Pages
folder,Add
,Razor Page
and chooseRazor Page - Empty
. then writeOptions
for name or whatever you want -
In
Options.cshtml.cs
deleteOnGet()
function and add these two lines inOptionsModel
class:[BindProperty] public IFormCollection Inputs { get; set; }
-
delete all code inside the
Options.cshtml
and add this code instead:@page @addTagHelper *, AutoOption.Pages @model OptionsModel <vc:option inputs="@Model.Inputs"></vc:option>
-
-
Config
inStartup.cs
set yourConnectionString
toOptionHelper.Config
OptionHelper.Config(typeof(Options), new SqlWrapper(Configuration.GetConnectionString("DefaultConnectionString")));
Now everything is ready. You can add any input to your options page just by writing properties in the options class.
Let's add a field of number and text type. You need to just add these two properties in your options class
public int PageCount { get; set; }
public bool AllowComment { get; set; }
Now build your project and go to https://localhost:44378/Options
Done.
If you want to have pretty titles, use Display
attribute at top of the properties
[Display(Name = "Page Count")]
public int PageCount { get; set; }
[Display(Name = "Allow Comment")]
public bool AllowComment { get; set; }
If you want to access the value of fields programmatically, write like this
int count = OptionHelper.Get<Options>().PageCount;
If you want to change the value of fields programmatically, write like this
OptionHelper.Set(() => OptionHelper.Get<Options>().PageCount, 10);
Also, you can add enum property and you will see a Select input equivalently. (like here)