Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core blazor中非布尔的条件属性_Asp.net Core_Blazor - Fatal编程技术网

Asp.net core blazor中非布尔的条件属性

Asp.net core blazor中非布尔的条件属性,asp.net-core,blazor,Asp.net Core,Blazor,我正在尝试使用复选框系统生成自己的组件,以了解是否需要该属性(类型为int/float等) 大概是 <MyComponent @bind-Value="ValueInt" @if(isMinInt == true ? Min="@MinInt" : String.Empty /> 编辑+解决方案2 现在使用@属性: <input type="checkbox" @bind="isMinInt" /> @if (isMinInt == true

我正在尝试使用复选框系统生成自己的组件,以了解是否需要该属性(类型为
int
/
float
等)

大概是

<MyComponent @bind-Value="ValueInt" 
             @if(isMinInt == true ? Min="@MinInt" : String.Empty />
编辑+解决方案2

现在使用
@属性

<input type="checkbox" @bind="isMinInt" />
@if (isMinInt == true) {
   <input type="number" @bind="MinInt" />
}

<MyComponent @bind-Value="ValueInt" @attributes="GetAttributesInt()" />

@code {
    private bool isMinInt = false;
    private int MinInt;

    private IDictionary<string, object> GetAttributesInt() {
       var dict = new Dictionary<string, object>() { };
       if (isMinInt)
         dict.Add("Min", MinInt);
       return dict;
    }
}
<input type="checkbox" @bind="isMinInt" />
@if (isMinInt == true) {
   <input type="number" @bind="MinInt" />
}

<MyComponent @bind-Value="ValueInt" @attributes="GetAttributesInt()" />

@code {
    private bool isMinInt = false;
    private int MinInt;

    private IDictionary<string, object> GetAttributesInt() {
       var dict = new Dictionary<string, object>() { };
       dict["Min"] = this.isMinInt ? MinInt : Int32.MinValue;
       return dict;
    }
}

@如果(isMinInt==true){
}
@代码{
private bool isMinInt=false;
私家侦探;
私有IDictionary GetAttributesInt(){
var dict=newdictionary(){};
dict[“Min”]=this.isMinInt?MinInt:Int32.MinValue;
返回命令;
}
}
我之所以使用
Int32.MinValue
是因为
MyComponent
对应于一个
,其中他的
min
绑定到我的
MinInt
,所以如果我用
0
代替
Int32.MinValue
,它将不允许我使用负数

我将在我的组件上有许多属性,我想使它更简单

你不能直接这么做。但是,作为演练,您可以使用
@attributes
动态绑定属性。例如:

<MyComponent @bind-Value="ValueInt" @attributes="@a_dictionary_expression"  />
[编辑]:下面是一种在一行中呈现属性的方法

<input @bind-Value="ValueInt" @attributes="@(isMinInt? new Dictionary<string,object>{ Min= @Min} : new Dictionary<string,object>{})" />
  • 第二次
    @attributes
    {}
    ,因为里面没有属性,它不会为它分配参数,因此
    MyComponent.Min
    保持不变
  • 要解决此问题,请按以下方式更改上述代码:

    private IDictionary<string, object> GetAttributesInt() {
        var dict = new Dictionary<string, object>() { };
        dict["Min"] = this.isMinInt ? MinInt : 0 ;
        return dict;
    }
    
    private IDictionary GetAttributesInt(){
    var dict=newdictionary(){};
    dict[“Min”]=this.isMinInt?MinInt:0;
    返回命令;
    }
    
    使用自定义函数时出错:无法在Int32类型的对象上设置属性“Min”。错误是:指定的强制转换无效。-->System.InvalidCastException:指定的强制转换无效。@请将其更改为
    dict.Add(“Min”,MinInt)谢谢,如果我的IsMini在之后没有返回为false,我如何删除它?@yToxide您不必手动删除它。每次更改数据时,您都应该得到一个新的字典,从而得到一个新的
    @attributes
    ,最后您将得到一个新的呈现的UI。始终遵循以下模式:
    更改数据-->UI更新
    它不起作用,当我取消选中复选框时,它将返回到false,但我的组件仍保留Min的值 <MyComponent @bind-Value="ValueInt" @attributes="getAttributes()" /gt; @code { ... private IDictionary getAttributes() { var dict = new Dictionary(){}; if(isMinInt) { dict.Add("Min", $"{MinInt}"); } // this will introduce a bug, see Edit2 for more details return dict ; } }
    <input @bind-Value="ValueInt" @attributes="@(isMinInt? new Dictionary<string,object>{ Min= @Min} : new Dictionary<string,object>{})" />
    
    MyComponent.Min=1;
    
    private IDictionary<string, object> GetAttributesInt() {
        var dict = new Dictionary<string, object>() { };
        dict["Min"] = this.isMinInt ? MinInt : 0 ;
        return dict;
    }