Is there a way to terminate MVC Razor @: line?

落花浮王杯 提交于 2019-12-11 02:34:04

问题


Consider this example:

<input type="checkbox" @if (flag) { @: checked } />

The problem is that the } /> is conceived by the Razor engine as part of the output string.

I've tried to use parenthesis but no luck.

Is there a way to terminate the @: operator in same line, or I'll have to split it to other line / use ternary operator?

Update

A very common use-case of my request can be adding a class if a value is true:

<div class='container @(active ? "active")'/>

So if the : of the ternary operator isn't present, it treats it treats it as a unary if operator, but that's just a wish...

Here's my suggestion on C# UserVoice.


回答1:


For single attribute you can use @value syntax, that also have special case for checkbox (see Razor quick reference ):

<input type="checkbox" checked="@flag" />

You also can use <text> syntax that provides explicit (instead of "up to end of the line") closing tag:

<input type="checkbox" @if (flag) { <text>checked</text> } />


来源:https://stackoverflow.com/questions/29090192/is-there-a-way-to-terminate-mvc-razor-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!