简体   繁体   中英

How to use C# inside an html tag in a razor view?

I am trying to hide an html row using inline css and C# conditionals and I am not getting the syntax correctly. (I know how to hide the row using jQuery but I want to use inline css)

My try: <tr @{if (Model.Loan.IsValid != "Y") {@:style="display:None;"}}>

Another try: <tr @(Model.Loan.IsValid != "Y"? "style=display: None;":"")> shows as <tr style="display:" none;="">

You can try it this way. Using the @: breaks out of the C# code so you can start using HTML in the middle of the C# statement.

<tr 
    @if(Model.Loan.IsValid != "Y") 
    {
        @:style="display:none;"
    }
>

<tr>

How about:

<tr @(Html.Raw(Model.Loan.IsValid != "Y"? "style=\"display: None;\"":""))>

Edit: I would suggest to use a class instead of a style however

One way of achieving this in one line is using a variable as follow:

<tr style="display: @{ var display = (Model.Loan.IsValid != "Y") ? "none" : ""; @display; }">  
</tr>

Rei.

I believe you're attempting to do the following:

@Html.DisplayFor(content => 
      Model.Loan.IsValid != "Y" ? "<tr style="display: none;"></tr>" : "<tr></tr>");

If you're attempting to be concise with a single line. A ternary, would work ideal in that instance. I used DisplayFor , but it might not be ideal based on your model, without more information. The Raw may be better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM