I worked on blazor server side .I face issue on blazor component user name is reset after submit bootstrap modal
I have bootstrap model have some controls i make submit to insert data but after insert i refresh page to display data
after that any variable as user name outside bootstrap modal reset value
so How to keep values outside bootstrap modal have values without reset after submit
user name before submit have name Michel
after submit on bootstrap modal become null
component name
/Servers/ServersNames
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true" data-backdrop="false" style="background-color: rgba(0, 0, 0, 0.5);">
<div class="modal-dialog modal-lg modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel" style="margin-right:100px;">
@ModalTitle
</h5>
<button type="button" style="background-color: white !important;" data-bs-dismiss="modal">X</button>
</div>
<EditForm Model="@sn">
<div class="modal-body">
<div class="d-flex flex-row bd-highlight mb-3">
<div class="p-2 w-100 bd-highlight">
<div class="form-group row">
<input type="hidden" class="form-control" @bind="@sn.ServerID" />
</div>
<div class="form-group row">
<label for="example-text-input" class="col-3 col-form-label required">Server Name</label>
<div class="col-9">
<input type="text" class="form-control" @bind="@sn.Server_Name"/>
</div>
</div>
<div class="form-group row">
<label for="example-text-input" class="col-3 col-form-label required">Server Type</label>
<div class="col-9">
<input type="hidden" class="form-control" @bind="@sn.ServerTypeId" />
<select class="form-select" @bind="@sn.ServerTypeId">
<option value="0">--Select--</option>
@foreach (var servertype in serverType)
{
<option value="@servertype.serverTypeId">
@servertype.serverType
</option>
}
</select>
</div>
</div>
<div class="form-group row">
<label for="example-text-input" class="col-3 col-form-label">Server Version</label>
<div class="col-9">
<input type="text" class="form-control" @bind="@sn.Version" />
</div>
</div>
<div class="form-group row">
<label for="example-text-input" class="col-3 col-form-label">Operating System</label>
<div class="col-9">
<input type="text" class="form-control" @bind="@sn.Operating_System" />
</div>
</div>
<div class="form-group row">
<div class="input-group mb-3">
<div class="input-group-text">
<InputCheckbox @bind-Value="@sn.IsActive" />
</div>
<label class="form-check-label" for="IsActive" style="padding-top:5px;">IsActive?</label>
</div>
</div>
@if (ModalTitle == "Add Server")
{
@* @onclick
= "CreateClick"*@
//disabled = "@(!context.Validate())"
<button type="submit" class="btn btn-primary" @onclick="CreateClick">
Create
</button>
}
else
{
<button type="button" class="btn btn-primary" @onclick="UpdateClick">
Update
</button>
}
</div>
</div>
</div>
</EditForm>
</div>
</div>
</div>
I navigate to same component after insert data to see data inserted
private async Task CreateClick()
{
//alertMessageShow = true;
//success = "Server Name Is Empty await swal.FireAsync(new SweetAlertOptions{
await swal.FireAsync(new SweetAlertOptions
{
Title = "Error",
Text = "Server Name Is Empty",
Icon = SweetAlertIcon.Error,
ShowCancelButton = false
// CancelButtonText="No"
});
return;
}
if (sn.ServerTypeId == 0 || sn.ServerTypeId == null)
{
//alertMessageShow = true;
//success = "Server Name Is Empty await swal.FireAsync(new SweetAlertOptions{
await swal.FireAsync(new SweetAlertOptions
{
Title = "Error",
Text = "Server Type Is Empty",
Icon = SweetAlertIcon.Error,
ShowCancelButton = false
// CancelButtonText="No"
});
return;
}
var deptobj = sn;
// deptobj = new ServerNamesClass() { server_Name = server_Name, server_Type = server_Type,serverTypeId=serverTypeId, operating_System = operating_System, version = version,isActive=IsActive };
await CheckExistOrNot(deptobj.Server_Name);
if (checkExist > 0)
{
// await JS.InvokeVoidAsync("alert", "Issue Server Exist Before");
await swal.FireAsync(new SweetAlertOptions{
Title="Confirmation",
Text = "Issue Server Exist Before",
Icon=SweetAlertIcon.Error,
ShowCancelButton=false
// CancelButtonText="No"
});
//var confirm = !string.IsNullOrEmpty(res.Value);
//if(!confirm)
//{
// context
//}
return;
}
else
{
var request = new HttpRequestMessage(HttpMethod.Post, config["API_URL"] + "ServerNames");
request.Content = new StringContent(JsonSerializer.Serialize(deptobj), null, "application/json");
var client = ClientFactory.CreateClient();
UserName = UserName;
var response = await client.SendAsync(request);
{
Title = "Confirmation",
Text = "Insert Success",
Icon = SweetAlertIcon.Success,
ShowCancelButton = false
// CancelButtonText="No"
});
navigationManager.NavigateTo("/Servers/ServersNames", true);
}
}