Styling Html.ActionLink is not supported

I'm struggling to style my links properly when combining CSS and Bootstrap with MVC. It seems like @Html.ActionLink is needed for redirection, but styling remains a challenge. Is there a way to style it effectively?

When using pure CSS, everything works fine:

<div class="mx-auto order-0">
        <a class="navbar-brand mx-auto" href="#">LIBRARY</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".dual-collapse2">
            <span class="navbar-toggler-icon"></span>
        </button>
    </div>

This is how I've set it up in MVC:

<div class="mx-auto order-0">
            <a class="navbar-brand mx-auto" href="/Index">
                @Html.ActionLink("Library", "Index", "MyTemplate", null, new { @class = "mx-auto order-0" })
            </a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".dual-collapse2">
                <span class="navbar-toggler-icon"></span>
            </button>
        </div>

Even after testing this setup on MVC, the styling issue persists:

@Html.ActionLink("Library", "Index", "MyTemplate", null, new { @class = "mx-auto order-0" })

Here's how my index.cshtml file is structured:

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_MasterLayout.cshtml";
}

<h2>Index</h2>

And here's the controller code:

    public class MyTemplateController : Controller
    {
        // GET: MyTemplate
        public ActionResult Index()
        {
            return View("Index");
        }

        public ActionResult About()
        {
            return View("About");
        }

        public ActionResult Contact()
        {
            return View("Contact");
        }
    }
}

In the HTML file:

    <!doctype html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <title>@ViewBag.title</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">

    <link rel="stylesheet" type="text/css" href="Content/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="Content/style.css">
    <link href="https://use.fontawesome.com/releases/v5.11.2/css/all.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Lato:wght@400;700&display=swap" rel="stylesheet"...

The styling issue persists despite various attempts. While adding inline styles could solve the problem temporarily, I prefer maintaining all styles in style.css for better organization. Any advice on resolving this dilemma would be greatly appreciated.

Answer №1

Looking for a way to implement this HTML code?

<div class="mx-auto order-0">
        <a class="navbar-brand mx-auto" href="#">LIBRARY</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".dual-collapse2">
            <span class="navbar-toggler-icon"></span>
        </button>
    </div>

In your MVC, ensure it matches the following structure:

<div class="mx-auto order-0">
            @Html.ActionLink("Library", "Index", "MyTemplate", null, new { @class = "mx-auto order-0" })
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".dual-collapse2">
                <span class="navbar-toggler-icon"></span>
            </button>
        </div>

Note that nesting Html.ActionLink within another anchor tag is not allowed due to link inside a link constraint.

Another approach would be (where the href of the a tag is the Razor Url helper):

<div class="mx-auto order-0">
        <a class="navbar-brand mx-auto" href="@Url.Action("Index", "MyTemplate")">LIBRARY</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".dual-collapse2">
            <span class="navbar-toggler-icon"></span>
        </button>
    </div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Spacing between products in Woocommerce product list

Welcome to my website! Check it out here: I am facing an issue with a long margin at the bottom of my woocommerce product list. I have tried using CSS to change it as shown below: .woocommerce .products ul, .woocommerce ul.products { margin-bot ...

What is the best method for aligning forms vertically within a page layout?

I'm working on designing a contact form, but I'm having trouble getting everything to align properly. Specifically, I can't seem to get the form elements to line up vertically and I also want the Message label to appear on the top left side ...

What is the best way to create an L shape using CSS?

I am currently working on achieving a unique style using CSS. https://i.sstatic.net/bJlok.png My progress so far: .file { width: 279px; height: 326px; background: linear-gradient(-135deg, transparent 66px, #A1A1A4 40px); position: relative; } ...

What impact does setting everything to position:relative have on a webpage?

I stumbled upon some CSS / HTML examples and came across this interesting snippet: *, *:before, *:after { position: relative; } It appears that implementing this rule can have a notable impact on the layout. What might be the rationale behind this ...

Combine the first name and last name to create the SelectCommand

My goal is to combine the TeacherForename and TeacherSurname fields in my dropdownlist so that users can easily see which Teacher they have selected. However, the combined fields will actually represent the TeacherID value. <div class="fields"> ...

Aligning icons and text vertically in a list

I've encountered this issue multiple times and I'm always unsure of the best approach to resolve it. Here is a screenshot for better context: This is what it should look like... .container { max-width: 360px; font-size: 16px; } .talking-poin ...

Show button image beyond the limits of the hyperlink

Is it possible to create a button with a background image that changes on hover and when active, while also having the active state display an image that extends beyond the anchor's bounds? Check out my sprite here: The top half of the sprite represe ...

Having difficulty viewing the images clearly

My JavaScript codes for scrolling images are not displaying all the images when viewed on Google Chrome. Is there a solution available? Below is the included JS code and CSS. data=[ [" images/img1.jpg"," Image1","pic01.jpg"], [" images/img2.jpg","Image2 " ...

Getting started with Reports in ASP.NET using C#

I am embarking on a project to create statistical reports. I have been instructed to utilize Crystal Reports if it is license-free, otherwise explore other free options. This will be my first foray into using reports in asp.net. Can someone provide guidan ...

In which orientation does the iPad come configured as standard?

There seems to be some confusion regarding the default orientation of the iPad. While some believe it is portrait (which is how I typically use my iPad), others argue that it is actually landscape. In my CSS, I have specific settings for targeting elemen ...

Experiencing issues with Bootstrap's width overflow while writing lengthy articles with numerous paragraphs

INTRODUCTION I am in the process of developing a website to exhibit my past work in blockchain and cybersecurity projects. Using bootstrap v5.1.3, I have successfully created two pages on my website so far, despite not being a seasoned front-end developer ...

Centered Fixed Upward Scrolling Div

Currently facing a challenge with my project involving a chat interface created in Angular. I am struggling with the CSS styling as the chat starts at the top and scrolls downward off-screen as the conversation progresses. Although I can scroll along with ...

Is it possible to have the detailsview checkbox automatically checked when inserting a new record?

When working with the DetailsView, specifically in its DefaultMode: insert, I encountered an issue regarding setting the default status of a checkbox. My aim is to have the checkbox checked by default, but still allow the user to change it to unchecked. Th ...

Creating a responsive card design with Material UI

Creating a responsive card layout for mobile applications is my current challenge const styles = { edit: { width: "40%", marginLeft: 270, background: "#76ff03" }, add: { width: "100%", background: "#18ffff", size: "large" ...

The website service is not functioning properly

Trying to implement web services, with the goal of displaying a message on the screen when a button is clicked or textbox text is changed. However, upon running the code below, no alert messages are appearing on the screen. Any suggestions? TEST.ASPX &l ...

Tips for expanding an md-nav-item in Angular Material

Need help on stretching the md-nav-item element illustration 1 ------------------------------------------------ | ITEM1 | ITEM 2 | ------------------------------------------------ illustration 2 ------------------------------------------------ | ...

Whenever I click on <a href="whatever.aspx"></a>, I would like the page to be displayed within the current page

This is the code I am working with: <asp:DataList ID="dlGallery" runat="server" RepeatLayout="Flow" Width="100%" CellPadding="4" ForeColor="#333333"> <AlternatingItemStyle BackColor="White" ForeColor="#284775" /> <FooterStyle BackColor ...

Customize the Header Background Color in React Table

I'm working on customizing a re-useable table component using vanilla CSS. I have defined six color variables and want users to be able to select one of them to change the table header background color. However, I am unsure how to make this color choi ...

Exploring the world of colored tab links in CSS

Check out this code: <!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <title>My Unique Page</title> <link rel="stylesheet" href="http://code.jquery.com/ui/1.8.3/themes/base/jquery-ui.css" /> ...

The MySQL Query Maker

To fetch data from a database for only one row and then generate separate pieces of information, use the following code: <?php session_start(); ob_start(); error_reporting(E_ERROR | E_WARNING | E_PARSE); ini_set("display_errors", 1); set_time_limit(1 ...