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

c#-从Blazor的SweetAlert返回假

(c# - Returning false from SweetAlert in Blazor)

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

编辑:找到了一种解决方法,但仍然想知道更好的方法

解决方法:
 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
        }

    }



原始问题:

我一直在尝试在项目中使用swal。

在我的剃须刀页面中,我想显示一个弹出窗口,根据其响应,我将做一些事情


@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
    }

单击“确定”可以正常工作,但单击“取消”将使应用程序崩溃。JSON value could not be converted to bool

如果我给出bool?而不是bool返回类型,则单击“取消”,它说null object cannot be converted to value type

也尝试使用Convert.ToBooleanConvert.ToInt32但没有用

如果用户点击取消,我该如何捕捉?

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

正如InvokeAsync文档所指出的那样,因为TValue是JSON可序列化的返回类型,无法序列化为bool

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