I am using .Net Core Razor pages to create word document using DocIO based on inputs in a form. At the end of the form, there is a submit button. If the form is filled out and passes validation (fields are not left blank, appropriate length, etc.), the function runs and the word document generates using the CreateDoc() function. In that instance, I want to display a Success toast.
If some part of the form is not filled out, the function returns the page with the error messages on the fields that were not properly filled out. In that case, I want to display a warning toast.
Right now, the success toast is displayed even if the validation fails. I can't figure out how to display a toast only after it passes validation.
This is what I have in the index.cshtml:
<ejs-toast id="element" title="Success!" showclosebutton="true" content="Document has been created." cssclass="e-toast-success" icon="e-success toast-icons" created="created">
<e-toast-position x="Right"></e-toast-position>
</ejs-toast>
<ejs-button id="SubmitBtn" content="Generate Document" cssclass="e-btn e-control"></ejs-button>
And this is what I have in the index.cshtml.cs:
public IActionResult OnPost()
{
if (ModelState.IsValid == false)
{
return Page();
}
return CreateDoc();
}
@using Microsoft.Extensions.Logging
@using ServerApp.Data
@using Syncfusion.Blazor.Notifications
<EditForm EditContext="@editContext" OnSubmit="@HandleSubmit">
<DataAnnotationsValidator />
<ValidationSummary />
<p>
<label>
Identifier:
<InputText @bind-Value="starship.Identifier" />
</label>
</p>
<button type="submit">Submit</button>
</EditForm>
<SfToast @ref="ToastObj" Title="@ToastTitle" Content="@ToastContent" CssClass="@ToastCssClass">
<ToastPosition X="Right" Y="Bottom"></ToastPosition>
</SfToast>
@code {
SfToast ToastObj;
private string ToastTitle = "";
private string ToastContent = "";
private string ToastCssClass = "";
protected override void OnInitialized()
{
editContext = new(starship);
}
private async Task HandleSubmit()
{
if (editContext.Validate())
{
this.ToastTitle = "This is a success toast";
this.ToastContent = "SUCCESS..!";
this.ToastCssClass = "e-toast-success";
await Task.Delay(50);
await this.ToastObj.ShowAsync();
}
else
{
this.ToastTitle = "This is a error toast";
this.ToastContent = "ERROR..!";
this.ToastCssClass = "e-toast-danger";
await Task.Delay(50);
await this.ToastObj.ShowAsync();
}
}
} |
This looks like Blazor. I'm using .Net Core Web Application (using Razor Pages).
namespace Razorpages.Entities
{
public class Account
{
[Required]
[MinLength(3)]
[MaxLength(10)]
public string Username { get; set; }
} } |
<form method="post" asp-page="index">
<table>
<tr>
<td>Username</td>
<td>
<input asp-for="@Model.account.Username" />
</td>
<td>
<span asp-validation-for="@Model.account.Username" class="error"></span>
</td>
</tr>
<tr>
<td> </td>
<td>
<input type="submit" value="Save" />
</td>
</tr>
</table>
</form> @if (@ViewData["Status"] != null)
{
<ejs-toast id="element" created="oncreated" content="@ViewData["Content"]" cssClass="@ViewData["CssClass"]" icon="e-success toast-icons">
<e-toast-position X="Right"></e-toast-position>
</ejs-toast>
} <script>
var toastObj;
function oncreated() { toastObj = this;
toastObj.show()
} </script> |
namespace Razorpages.Pages
{
public class IndexModel : PageModel
{
[BindProperty]
public Account account { get; set; } public void OnGet( )
{
account = new Account();
if (HttpContext.Request.Query["status"].Count > 0)
{
var value = HttpContext.Request.Query["status"][0];
if (value != "")
{
if (value == "true")
{
ViewData["Content"] = "Success";
ViewData["CssClass"] = "e-toast-success";
}
else if (value == "false")
{
ViewData["Content"] = "Failed";
ViewData["CssClass"] = "e-toast-danger";
}
ViewData["Status"] = true;
}
else
{
ViewData["Status"] = false;
}
}
} public IActionResult OnPost(Account account) {
if (ModelState.IsValid == true)
{
string url = string.Format("Index?status={0}", "true");
//You can call your controller page. return Redirect(url);
}
else
{
string url = string.Format("Index?status={0}", "false");
return Redirect(url);
}
}
} } |