C# MVC中带Razor的条件OnClick#

C# MVC中带Razor的条件OnClick#,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我有一个名为wURL的变量,它包含一个URL。如果URL为空,我希望我的TD中没有onclick事件。我的问题是如何在razor语法中实现这一点: <td class="" onclick="window.open('@wURL', '_blank')"> 我想应该是这样的: <td class="" (@wURL ?){ onclick="window.open('@wURL', '_blank')"}> 但这不起作用。写这个的正确方法是什么?编辑: 我将把

我有一个名为wURL的变量,它包含一个URL。如果URL为空,我希望我的TD中没有onclick事件。我的问题是如何在razor语法中实现这一点:

<td class="" onclick="window.open('@wURL', '_blank')">

我想应该是这样的:

<td class="" (@wURL ?){ onclick="window.open('@wURL', '_blank')"}>

但这不起作用。写这个的正确方法是什么?

编辑: 我将把这个留给那些想用razor显示不同元素切换条件的人:

@if(condition){
    <td onclick="window.open('@wURL','_blank')">...</td>
}
else{
    <td >...</td>
}
@if(条件){
...
}
否则{
...
}
但是,如果您只希望其中一部分不同,那么您会发现其他答案更合适。

编辑: 我将把这个留给那些想用razor显示不同元素切换条件的人:

@if(condition){
    <td onclick="window.open('@wURL','_blank')">...</td>
}
else{
    <td >...</td>
}
@if(条件){
...
}
否则{
...
}
但是,如果您只希望其中的一部分不同,那么您会发现其他答案更合适。

尝试以下方法:

<td @(Html.Raw(wURL == null ? "" : $"onclick=\"window.open('{wURL}', '_blank')\"")) >

试试这个:

<td @(Html.Raw(wURL == null ? "" : $"onclick=\"window.open('{wURL}', '_blank')\"")) >

{string oc=wURL==null?”:“onclick=window.open(“+@wURL+”,“\u blank”);}
应提问者的要求,我将把三元结构分解为一个更简单的if-else语句进行澄清:

@{ // start a c# block
    string oc; // create a string variable
    if wURL == null { //before I used a "ternary" conditional, but this compiles to the same thing
        oc = ""; // if wURL is null set oc to empty string, so nothing will be written in the tag 
    } 
    else 
    { 
        oc = "onclick=window.open('" + @wURL + "','_blank')"; // set the onclick to open a window with the wURL variable
    }
}
<td class = "" @oc> 
<!-- If the oc variable is empty, nothing will be written after class modifier, otherwise you'll get the onclick statement -->
@{//启动一个c#块
string oc;//创建字符串变量
如果wURL==null{//在我使用“三元”条件之前,但这编译成相同的东西
oc=”“;//如果wURL为null,则将oc设置为空字符串,这样标记中就不会写入任何内容
} 
其他的
{ 
oc=“onclick=window.open”(“++@wURL+”,“\u blank”);//设置onclick以打开带有wURL变量的窗口
}
}
要查看如何将原始示例中的if-else语句压缩为三元语句,请查看。

{string oc=wURL==null?”:“onclick=window.open(“+@wURL+”,“\u blank”);}
应提问者的要求,我将把三元结构分解为一个更简单的if-else语句进行澄清:

@{ // start a c# block
    string oc; // create a string variable
    if wURL == null { //before I used a "ternary" conditional, but this compiles to the same thing
        oc = ""; // if wURL is null set oc to empty string, so nothing will be written in the tag 
    } 
    else 
    { 
        oc = "onclick=window.open('" + @wURL + "','_blank')"; // set the onclick to open a window with the wURL variable
    }
}
<td class = "" @oc> 
<!-- If the oc variable is empty, nothing will be written after class modifier, otherwise you'll get the onclick statement -->
@{//启动一个c#块
string oc;//创建字符串变量
如果wURL==null{//在我使用“三元”条件之前,但这编译成相同的东西
oc=”“;//如果wURL为null,则将oc设置为空字符串,这样标记中就不会写入任何内容
} 
其他的
{ 
oc=“onclick=window.open”(“++@wURL+”,“\u blank”);//设置onclick以打开带有wURL变量的窗口
}
}

查看原始示例中if-else语句如何压缩到三元结构中。我已经尝试过了,但是Razor很难找到结束符,所以我宁愿使用内联方法,嗯,你是对的,如果你有像
这样的自动关闭标签,或者你想拥有完全不同的TDs,这种方法就可以了。我已经尝试过了,但是Razor很难找到关闭,所以我宁愿使用内联方法,嗯,你是对的,如果您有像
这样的自动关闭标签,或者您希望有完全不同的TDs,则此功能可以工作。您能解释一下答案吗?是的。我会把它分解成更小的部分。我已经把三元条件转换成一个更清晰的if-else语句,这样你就可以看到它是如何工作的。你能解释一下答案吗?是的。我将把它分解成更小的部分我已经把三元条件转换成了一个更清晰的if-else语句,这样你就可以看到它是如何工作的。这就是我想要的,但它需要我的wURL,在每个/forwardslash上分解URL,在spaces@JoeStellato更新了我的答案。请检查这是我正在寻找的,但它需要我的wURL并在每个/forwardslash上断开URL,在spaces@JoeStellato更新了我的答案。请查收