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.ToBoolean
,Convert.ToInt32
但没有用
如果用户点击取消,我该如何捕捉?
正如InvokeAsync
文档所指出的那样,因为TValue
是JSON可序列化的返回类型,无法序列化为bool
。
// Type parameters: // TValue: // The JSON-serializable return type.