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