Blazor InputText:有条件地呈现属性

时间:2019-09-20 00:03:55

标签: asp.net-core blazor blazor-server-side

Blazor vRC1

我正在寻找一种关于如何有条件地在<InputText>(或与此相关的任何输入组件)中呈现属性的简单技术。在MVC Razor中,这曾经很简单,您只需在@(...)语句中编写条件逻辑。现在,在Razor语法中编写@(...)具有不同的含义。

例如,我想有条件地为autofocus输出InputText HTML属性。

<InputText 
 @bind-Value="@TextProperty"
 @(MyModel.isAutoFocus ? "autofocus" : "") <-  This is invalid razor syntax!
/>

3 个答案:

答案 0 :(得分:5)

您可以尝试以下代码:

st = "PREDICTED: LOW QUALITY PROTEIN: uncharacterized protein LOC107059219 [Solanum pennellii]"
st.split(":")[-1].split("[")[0].strip()

请参阅https://github.com/aspnet/AspNetCore/issues/10122

答案 1 :(得分:3)

这可以通过@attributes标记来实现。 See more here

基本上,您可以将@attributes标记添加到InputText。这可以绑定到参数或方便的小方法。

if fish_type == "carnivorous":
    fish_size = str(input("Do you have smaller fish already? "))
    if fish_size == "yes":
        print("This is a bad idea! It'll eat the little ones!")
    if fish_size == "no":
        print("Great! Enjoy!")
if fish_type == "salt water":
    print("Wow, you're a fancy fish parent!")
if fish_type == "community":
    fish_number = int(input("How many fish of this species do you already have?\
 "))
    if fish_number < 3:
        print("You should get more than one fish!")
    else:
        print("Yay, more friends!")
else:
    print("I don't think that's a type of fish; maybe you're looking for a \
lizard?")

答案 2 :(得分:1)

事实证明,如果属性的值为falsenull

,Blazor将不会渲染该属性。

https://docs.microsoft.com/en-us/aspnet/core/blazor/components?view=aspnetcore-3.0#conditional-html-element-attributes

  

HTML元素属性基于.NET有条件地呈现   值。如果值为false或null,则不呈现该属性。如果   值为true时,属性将最小化。

<InputText @bind-Value="@TextProperty" autofocus="@MyModel.isAutoFocus" />