【问题标题】:Returning false from SweetAlert in Blazor从 Blazor 中的 SweetAlert 返回 false
【发布时间】: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但没有用

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

【问题讨论】:

    标签: c# .net-core blazor sweetalert


    【解决方案1】:

    因为InvokeAsync 文档指出TValue 是JSON 可序列化的返回类型,不能序列化为bool

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

    【讨论】:

      猜你喜欢
      • 2015-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-04
      相关资源
      最近更新 更多