ActionLink Razor – How To Add CSS Class

Razor

If you are a web developer, you probably know that Razor is a popular programming syntax used in ASP.NET web applications.

It allows developers to write HTML markup and C# code in the same file. It makes it easy to create dynamic web pages. In this article, we will focus on how to add a CSS class to an ActionLink in Razor.

We will discuss the basics of Razor syntax. How to create an ActionLink, and finally, how to add a CSS class to it. So, let’s get started!

1. Understanding Razor Syntax

Before we dive into adding a CSS class to an ActionLink in Razor, it’s important to have a basic understanding of Razor syntax.

Razor is a server-side markup language that allows you to write C# code inline with your HTML markup.

Razor syntax is designed to be lightweight, easy to read, and easy to write.

Here are a few key points to keep in mind when working with Razor syntax:
  1. The razor code is enclosed in @{ } blocks.
  2. Razor expressions are enclosed in @( ) blocks.
  3. Razor comments start with @* and end with *@.

With that in mind, let’s move on to creating an ActionLink in Razor.

2. Creating an ActionLink in Razor

An ActionLink is a method in Razor that generates an HTML anchor tag, or <a> tag, which can be used to create hyperlinks between different pages in your web application.

Show More Show Less Using CSS

Horizontal Timeline CSS Responsive -Guide

CSS Mask Div with Another Div- Complete Guide

Here’s how you can create an ActionLink in Razor:

@Html.ActionLink("Link Text", "ActionName", "ControllerName")
  1. "Link Text" is the text that will be displayed as the link.
  2. "ActionName" is the name of the action method in your controller that you want to link to.
  3. "ControllerName" is the name of the controller that contains the action method.

Now that we know how to create an ActionLink, let’s move on to adding a CSS class to it.

3. Adding a CSS Class to ActionLink in Razor

Adding a CSS class to an ActionLink in Razor is quite simple. All you need to do is add a fourth parameter to the @Html.ActionLink method, which specifies the CSS class you want to apply to the link. Here’s an example:

<mark style="background-color:#fcb900" class="has-inline-color">@Html.ActionLink("Link Text", "ActionName", "ControllerName", null, new { @class = "my-css-class" })</mark>
  • "my-css-class" is the name of the CSS class you want to apply to the link.

Note that the fourth parameter is set to null. This is because we don’t need to specify any additional route values for this link. If you do need to specify additional route values, you can do so by passing them as an anonymous object in the fourth parameter.

And that’s it! You have successfully added a CSS class to an ActionLink in Razor.

4. Conclusion

In this article, we learned how to add a CSS class to an ActionLink in Razor. We started by understanding the basics of Razor syntax, then moved on to creating an ActionLink and finally adding a CSS class to it. We hope this article was helpful and provided you with a better understanding of how to use Razor syntax in your web applications.

HTML Boilerplate: A Comprehensive Guide

What Is The Difference Between Java And Javascript?

Amazon Web Development Engineer Interview Questions