Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Razor conditional rendering an element

How does one conditionally render an HTML element in Razor 2?

For instance, suppose I had the tag

    <div class="someclass">
         <p>@somevalue</p>
    </div>

And I wanted to suppress the <-div-> tag from rendering if the value of @somevalue was equal to 1. Is there a simple way to do this in Razor similar to how I might "hide" the <-div-> tag with Knockout.js in a browser, where I might :

    <div class="someclass" data-bind="showWhenTrue: someValue != 1">
         <p data-bind="text: someValue"></p>
    </div>

At the moment, the best Razor alternative I have is to do this:

      @if (someValue != 1) {
         <div class="someclass">
               <p>@somevalue</p>
         </div>
      }
like image 522
user18539 Avatar asked May 15 '14 03:05

user18539


People also ask

Can I render MVC Razor views directly to string?

Typically, Razor rendering in ASP.NET MVC is reserved purely for view rendering and generation of HTML output as part of an MVC request. But, as I describe in this article, it's also possible to render MVC Razor views directly to string in order to capture the output and to render those views outside of the direct context of an MVC Web request.

How to use conditional attributes in Razor View?

Conditional Attributes in Razor respond to boolean values, which can be very useful for say checkboxes. A checkbox has a checked attribute that you set to checked when the checkbox is supposed to be checked; Otherwise the attribute is not included. This is perfect for Conditional Attributes. Add the following code block and HTML to your Razor View.

What is razor engine in ASP NET MVC?

ASP.NET MVC and the Razor Engine that is used to render ASP.NET MVC views combine to make an excellent text and HTML template-rendering engine. Together, they make it easy to merge text, model data, and code to produce rich HTML output.

Can We render layouts based on a condition in MVC?

Using the method described above we can render layouts based on a condition in ASP.NET MVC. Microsoft Consultant. Office 365 / SharePoint / PowerApps / Power Automate / Azure. #SharePoint Architect


1 Answers

There are many ways to do this. First, it should be noted that your knockout code doesn't actually remove the html from output, it just sets its display to hidden.

The razor code you have actually removes the code from the rendered HTML, so that's a very different thing.

To answer your question, we need to know what it is you're trying to achieve. If you just want to hide the display, you can simply do something like this:

<div class="someclass" style="display: @{ somevalue == 1 ? @:"none" : @:"block" };">
     <p>@somevalue</p>
</div>

You could also do it with a class:

<div class="someclass @{ somevalue == 1 ? @:"HideMe" : @:"ShowMe" }">
     <p>@somevalue</p>
</div>

If you want to remove the code from the output, then you can just do what you've done.. i'm mot sure what you find so objectionable about it... but if you want other alternatives, you could create an Html helper, you could use a razor helper, you could use a Display or EditorTemplate....

The list is actually quite long and i'm just scratching the surface...

like image 153
Erik Funkenbusch Avatar answered Sep 26 '22 21:09

Erik Funkenbusch