Files
Mf/Mf.Razor/Components/Alter.razor
T
2026-04-29 00:53:30 +03:30

224 lines
7.3 KiB
Plaintext

@using System.Reflection
@inherits BaseForm
<h3>Alter</h3>
<EditForm OnValidSubmit="Success" Model="Type">
<div>
<DataAnnotationsValidator />
<ValidationSummary />
@foreach (var p in Properties)
{
@if (p.PropertyType.Name == "Int32")
{
<div class="form-floating mb-3">
<InputNumber id="@nameof(p.Name)" @bind-Value="intValues.Where(v => v.PName == p.Name).Single().Value" placeholder="placeholder" class="form-control" />
<label for="@nameof(p.Name)">@p.GetDisplayName()</label>
</div>
}
else if (p.PropertyType.Name == "String")
{
<div class="form-floating mb-3">
<InputText id="@nameof(p.Name)" @bind-Value="Values.Where(v => v.PName == p.Name).Single().Value" placeholder="placeholder" class="form-control" />
<label for="@nameof(p.Name)">@p.GetDisplayName()</label>
</div>
}
else if (p.PropertyType == typeof(Nullable<int>))
{
<div class="form-floating mb-3">
<InputNumber id="@nameof(p.Name)" @bind-Value="n_intValues.Where(v => v.PName == p.Name).Single().Value" placeholder="placeholder" class="form-control" />
<label for="@nameof(p.Name)">@p.GetDisplayName()</label>
</div>
}
else if (p.PropertyType.Name == "DateTime")
{
<div class="form-floating mb-3">
<input id="@nameof(p.Name)" @bind="DateTimeValues.Where(v => v.PName == p.Name).Single().Value" placeholder="placeholder" class="form-control" />
<label for="@nameof(p.Name)">@p.GetDisplayName()</label>
</div>
}
else if (p.PropertyType.Name == "Boolean")
{
<div class="form-floating mb-3">
<input id="@nameof(p.Name)" @bind="BoolValues.Where(v => v.PName == p.Name).Single().Value" placeholder="placeholder" class="form-control" />
<label for="@nameof(p.Name)">@p.GetDisplayName()</label>
</div>
}
}
</div>
<button class='btn btn-sm @((IsUpdate)? "btn-warning" : "btn-success")'>ثبت</button>
</EditForm>
@code {
[Parameter]
public Type Type { get; set; } /* = typeof(SubAsil.Models.Users.User); */
[Parameter]
public object Data { get; set; }
[Parameter]
public EventCallback<object> OnChange { get; set; }
[Parameter]
public EventCallback<object> OnDone { get; set; }
public List<PropertyInfo> Properties { get; set; } = new();
List<Mmodel<bool?>> BoolValues = new();
List<Mmodel<string>> Values = new();
List<Mmodel<DateTime?>> DateTimeValues = new();
List<Mmodel<int>> intValues = new();
List<Mmodel<int?>> n_intValues = new();
List<bool> boolValues = new();
object model = new object();
bool IsUpdate = false;
protected override async Task OnInitializedAsync()
{
model = Activator.CreateInstance(Type);
if (Data != null)
{
model = Data;
IsUpdate = true;
}
//var Users = await HttpClient.GetFromJsonAsync<List<object>>("api/generic/get?id=" + Type.AssemblyQualifiedName);
// var user = Users[0];
// model = user;
Properties = Type.GetProperties().ToList();
Properties = Properties.Where(p => !PropertyFilters.Any(pp => pp == p.Name)).ToList();
foreach (var p in Properties)
{
string typeName = p.PropertyType.Name;
// ServiceLocator.Log(p.PropertyType.ToString());
// ServiceLocator.Log(typeName);
if (p.PropertyType == typeof(Nullable<int>))
{
n_intValues.Add(new Mmodel<int?>() { Value = (p.GetValue(model) != null) ? int.Parse(p.GetValue(model).ToString()) : null, PName = p.Name });
}
// if (p.PropertyType == typeof(Nullable<DateTime>))
// {
// DateTimeValues.Add(new Mmodel<DateTime?>() { Value = (p.GetValue(model) != null) ? Convert.ToDateTime(p.GetValue(model)) : null, PName = p.Name });
// }
if (typeName == "DateTime")
{
DateTimeValues.Add(new Mmodel<DateTime?>() { Value = (p.GetValue(model) != null) ? Convert.ToDateTime(p.GetValue(model)) : null, PName = p.Name });
}
if (typeName == "Boolean")
{
BoolValues.Add(new Mmodel<bool?>() { Value = (p.GetValue(model) != null) ? Convert.ToBoolean(p.GetValue(model)) : null, PName = p.Name });
}
// if (typeName.Contains("Nullable"))
// {
// if (p.PropertyType.UnderlyingSystemType.ToString().Contains("Int32"))
// {
// n_intValues.Add(new Mmodel<int?>() { Value = (p.GetValue(model) != null) ? int.Parse(p.GetValue(model).ToString()) : null, PName = p.Name });
// }
// }
if (p.PropertyType.Name == "String")
{
Values.Add(new Mmodel<string>() { Value = (string)p.GetValue(model), PName = p.Name });
}
else if (p.PropertyType.Name == "Int32")
{
intValues.Add(new Mmodel<int>() { Value = int.Parse(p.GetValue(model).ToString()), PName = p.Name });
}
}
}
[Inject]
public IJSRuntime Js { get; set; }
async void Success()
{
//await Js.Alert(model.ToString());
foreach (var item in Properties)
{
if (item.PropertyType == typeof(string))
{
item.SetValue(model, Values.Where(v => v.PName == item.Name).Single().Value);
}
if (item.PropertyType == typeof(Int32))
{
item.SetValue(model, intValues.Where(v => v.PName == item.Name).Single().Value);
}
if (item.PropertyType == typeof(Nullable<Int32>))
{
item.SetValue(model, n_intValues.Where(v => v.PName == item.Name).Single().Value);
}
if (item.PropertyType == typeof(Nullable<DateTime>))
{
var find = DateTimeValues.Where(v => v.PName == item.Name).SingleOrDefault()?.Value;
item.SetValue(model, DateTimeValues.Where(v => v.PName == item.Name).SingleOrDefault()?.Value);
}
}
if (IsUpdate)
{
//var result = HttpClient.PostAsJsonAsync<PostModel>("api/generic/update", new PostModel() { type = this.Type.AssemblyQualifiedName, model = this.model });
}
else
{
//var result = HttpClient.PostAsJsonAsync<PostModel>("api/generic/add", new PostModel() { type = this.Type.AssemblyQualifiedName, model = this.model });
}
await OnDone.InvokeAsync(model);
//await OnChange.InvokeAsync(model);
}
public int GetNumber(PropertyInfo property)
{
return Properties.IndexOf(property);
}
public class Mmodel<T>
{
public T Value { get; set; }
public string PName { get; set; }
}
public List<string> PropertyFilters = new List<string> { "CreateDate", "UpdateDate" };
}