@page "/country/create" @page "/country/edit/{id:long}" @page "/{locale}/country/create" @page "/{locale}/country/edit/{id:long}" @inject ILogger Logger @attribute [Authorize] 管理后台 系统配置 国家管理
@if (Id > 0 && languageList.Count() > 0) { 标准 @foreach (var item in languageList) { @item.Value } } 允许发货 启用
@code { [Parameter] public string Locale { get; set; } = string.Empty; [Parameter] public long Id { get; set; } [SupplyParameterFromForm] CountryModel model { get; set; } = new(); Form editform = null!; CountryLocalizedModel country = new(); List languageList = new(); bool pageLoading = false; bool saving = false; protected override void OnInitialized() { base.OnInitialized(); } protected override void OnParametersSet() { _ = LoadLanguage(); if (Id > 0) { LoadData(); } base.OnParametersSet(); } async Task LoadLanguage() { var url = $"/api/language/enabled"; var apiResult = await HttpService.Get>>(url); if (apiResult.Success) { languageList = apiResult.Data ?? new List(); StateHasChanged(); } } async void LoadData() { pageLoading = true; var url = $"/api/country/detail?id={Id}"; var apiResult = await HttpService.Get>(url); if (apiResult.Success) { if (apiResult.Data == null) { Navigation.NavigateTo($"/country/create"); } else { country = apiResult.Data; model = apiResult.Data.Adapt(); } } else { Navigation.NavigateTo($"/country/create"); } pageLoading = false; StateHasChanged(); } async void OnFormFinishAsync() { if (editform.Validate()) { saving = true; var url = $"api/country/save"; var result = new ApiResult(); result = await HttpService.Post>(url, model); if (result.Success) { saving = false; await ModalService.InfoAsync(new ConfirmOptions() { Title = "提示", Content = "数据提交成功!" }); Navigation.NavigateTo($"/country/list"); } else { saving = false; await ModalService.ErrorAsync(new ConfirmOptions() { Title = "服务异常", Content = result.Message }); } } } void OnLanguageTabChange(string key) { if (key != "0") { model.LanguageId = key; var data = country.Locales.Where(p => p.LanguageId == key.ToInt()).ToList(); if (data.Any()) { var name = nameof(model.Name); model.Name = data.SingleOrDefault(p => p.Key == name)?.Value ?? ""; } else { model.Name = string.Empty; } } else { model = country.Adapt(); model.LanguageId = key; } } }