Currently I am trying to write the data from my list, to a JSOn file. I have already read a very similar post to this issue, only for some reason it does not seem to work for me.
This is my model:
namespace ERP.Classes
{
public class User_Role
{
public string RoleId { get; init; }
public string RoleName { get; set; }
public string RoleDescription { get; set; }
public List<User> Users { get; set; }
public User_Role(string roleName, string roleDescription)
{
RoleId = Guid.NewGuid().ToString("N");
RoleName = roleName;
RoleDescription = roleDescription;
Users = new List<User>();
}
}
}
This is my razor page, where i want to create this list and save the JSON file:
@page "/user_roles"
@using ERP.Classes
@using Microsoft.AspNetCore.Components.Forms
<PageTitle>User Roles</PageTitle>
<h1>User Roles</h1>
<EditForm Model="@_userRoles" OnValidSubmit="@createRoleName">
<InputText id="roleName" @bind-Value="_roleName"/>
<InputText id="roleDescription" @bind-Value="_roleDescription"/>
<button type="submit">Create user role</button>
</EditForm>
<p>@_confirmation</p>
<table>
<tr>
<th><h5>Role Id</h5></th>
<th><h5>Role Name</h5></th>
<th><h5>Role Description</h5></th>
</tr>
@foreach(User_Role list in _userRoles)
{
<tr>
<td>@list.RoleId</td>
<td>@list.RoleName</td>
<td>@list.RoleDescription</td>
</tr>
}
</table>
// Button to call the method
<button onclick="@saveData">Save</button>
@code {
private string _confirmation = "";
private string _roleName = "";
private string _roleDescription = "";
private List<User_Role> _userRoles = new List<User_Role>();
private void createRoleName()
{
if(roleExists(_userRoles,_roleName))
{
_confirmation = "Role already exists";
}
else
{
User_Role _userRole = new User_Role(_roleName,_roleDescription);
_userRoles.Add(_userRole);
_confirmation = "Role created";
}
}
private List<User_Role> loadRoles()
{
return _userRoles;
}
// Calling the SaveData class to send over the data
private void saveData()
{
SaveData<User_Role> _data = new SaveData<User_Role>();
_data.SavetoJson("userRoles.txt",_userRoles);
}
private bool roleExists(List<User_Role> user_Roles, string currentRoleName)
{
bool hasRole = false;
foreach(var item in user_Roles)
{
if(item==null)
{
hasRole = false;
}
else if(item.RoleName == currentRoleName)
{
hasRole = true;
}
}
return hasRole;
}
}
This is the class with the save functionality:
using System.Collections.Generic;
using Newtonsoft.Json;
namespace ERP.Classes
{
public class SaveData <T>
{
public void SavetoJson(string filename, List<T> dataList)
{
using (StreamWriter file = File.CreateText(@"C:\Data\" + filename))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, dataList);
}
}
}
}
The reason for making my SaveData class generic, is so that i can use this for all my other models aswell. To prevent repetitive coding. But for some reason it does not create the json file. I have tried it with both ".txt" and ".json". I am quite new to programming, so excuse me if this has been answered already. But I was unable to find it.