Warm tip: This article is reproduced from serverfault.com, please click

Returning false from SweetAlert in Blazor

发布于 2020-11-30 00:14:38

Edit: Found a workaround but still would like to know a better way

Workaround:
 async Task Delete(string userId)
    {
        var boolval = false;

        var modalresponse = await js.InvokeAsync<object>("swal", "Are you sure", "", "error", new { buttons = true });

        if(modalresponse == null)
        {
            boolval = false;
        }
        else if (modalresponse.GetType().IsValueType)
        {
            boolval = true;
        }

        if (boolval)
        {
           // delete user
        }
        else
        {
           // do something else
        }

    }



Original Question:

I've been trying to use swal in my project.

In my razor page, I'm trying to show a popup and according to its respond I'll do something


@inject IJSRuntime js

    var result = await js.InvokeAsync<bool>("swal", new
    {
        title = "Some title",
        text = "Some text",
        icon = "warning",
        buttons = true,
    });

    if(result) {
        // do something
    }
    else {
       // do something else
    }

Clicking OK works fine but clicking cancel crushes the app. Saying JSON value could not be converted to bool

If I give bool? instead of bool for a return type then clicking cancel, it says null object cannot be converted to value type

Also tried to use Convert.ToBoolean and Convert.ToInt32 but no use

How can I catch if user clicks cancel?

Questioner
Kaan Taze
Viewed
0
nAviD 2020-12-04 02:07:44

Because as InvokeAsync documentations point out the TValue is a JSON-serializable return type which can not be serialized to bool.

    // Type parameters:
    //  TValue:
    //  The JSON-serializable return type.