Using Css to Style an Image within Html.ActionLink

 Html.ActionLink("", "ActionResult", new { CustomerId= DataBinder.Eval(c.DataItem, "CustomerId") }, new { target = "_blank", @style = "background-image:url(/Image/edit.png); width:50px; height:30px;" }));

Hello there,

I am facing an issue setting an image for my action link as it always appears empty.

Could someone advise me on how to set an image to an html.actionlink?

I am looking to have the action link display as an image (like an image button).

Please refrain from suggesting the usage of a href.

Your assistance in this matter is greatly appreciated.

Thank you.

Answer №1

It has been noted in the comments that your image is not displaying because there is no text provided and your element is set to display as inline. Without any text, the element has no size, even if width or height properties are specified in the element's style. To resolve this issue, change the element to display as inline-block:

(Html.ActionLink(
    "",
    "ActionResult",
    new {
        CustomerId= DataBinder.Eval(c.DataItem, "CustomerId")
    },
    new {
        target = "_blank",
        @style = "
            background-image:url(/Image/edit.png);
            display:inline-block;
            width:50px;
            height:30px;
        "
    })
);

Answer №2

I recently came across a handcrafted Html Helper online that I've been using. Here's the code snippet:

/// <summary>
/// Custom Html helper to showcase an image.
/// </summary>
/// <param name="_helper"></param>
/// <param name="_url"></param>
/// <param name="_altText"></param>
/// <param name="_clickMethod"></param>
/// <param name="_htmlAttributes"></param>
/// <returns></returns>
public static MvcHtmlString ImageClick(this HtmlHelper _helper, string _id, string _url, string _altText, string _clickMethod, object _htmlAttributes)
{
    TagBuilder builder = new TagBuilder("image");

    var path = _url.Split('?');

    string pathExtra = "";

    if (path.Length > 1)
    {
        pathExtra += "?" + path[1];
    }

    builder.Attributes.Add("src", VirtualPathUtility.ToAbsolute(path[0]) + pathExtra);
    builder.Attributes.Add("alt", _altText);
    if (!String.IsNullOrEmpty(_clickMethod))
    {
        builder.Attributes.Add("onclick", _clickMethod + "()");
    }

    builder.GenerateId(_id);

    builder.MergeAttributes(new RouteValueDictionary(_htmlAttributes));
    return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
}

Hopefully this code snippet proves to be useful for you!

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

The contents table remains fixed in the top right corner as you scroll

I have developed an Angular app with a table-of-contents component that only displays two items. The code for the script is as follows: ts import { Component, OnInit } from '@angular/core'; import { pdfDefaultOptions } from 'ngx-extended-p ...

Is it possible to adjust positioning using just CSS?

I'm attempting to arrange these elements as shown in the image below: https://i.stack.imgur.com/tWy1I.png https://jsfiddle.net/ut1mgcLb/ HTML <div class="object1"> <div class="snax-voting-container-body"> ...

Customizing the navbar to be inverse on each webpage using CSS, PHP, and Bootstrap

Dealing with a Bootstrap navbar across multiple pages on my website is becoming a hassle. Every time I need to make a change, I find myself jumping from one page to another just to update it. My objective is to have the ability to edit the navbar in one ce ...

Dynamic parallax or stacked vertical text animation

I am attempting to replicate the text effect found on . The text is centered both vertically and horizontally within each div, but as you scroll down, the top position shifts creating a parallax effect. As you continue scrolling, the text transitions to th ...

Sticky full-height slider with horizontal scrolling

I've been working on creating a horizontal slider animation that smoothly scrolls, inspired by the one showcased on The slider on weltio remains sticky as you scroll, showcasing images and content within. On my page, I have a lot of content and my g ...

Utilizing jQuery's each() function to create a seamless loop of background images in a

Struggling with looping the background image in a slick slider? Check out the code snippet below. var json = [ { "img_url": "https://via.placeholder.com/500x500?text=1" }, { "img_url": "https://via.placeholder.com/500x500?text=2" }, { "img_url": "ht ...

Enhancing Image Quality in JavaFx's Scene Builder

Hey everyone! I've been using JavaFx scene builder to create a user interface with some png images. Up to now, I've been using labels and enlarging them to display the pictures, but this solution isn't ideal because I need to create multipl ...

The Laravel error message "Attempting to access 'index()' on a null object"

After generating a model in Laravel, I attempted to migrate the table using 'php artisan migrate', but encountered an error message on the terminal: "Call to a member function index() on null" ...

Button positioned in the upper right corner

Trying to create a navigation menu like the one shown in the image below. https://i.sstatic.net/N9BQ5.png I've successfully styled the right corner, but I'm struggling to extend the top left corner. https://i.sstatic.net/cL0ZK.png Below is th ...

Graphic selectors: a new take on radio buttons

I've been attempting to make this work, but it's not functioning correctly. Below is the CSS code: .input_hidden { position: absolute; left: -9999px; } .selected { background-color: #000000; } #carte label { display: inline-bl ...

Retrieve data from an online JSON file

I am still learning about working with json and would appreciate some guidance. The data file I need to access is located at this url - I would like to display it in the following format: <ul id="smenu"> <li></li> </ul> Cou ...

The printed PDF of a webpage does not display HTML table background colors in Chrome or Firefox

Is there a way to change the colors of table cells in an HTML table when exporting to PDF? I'm creating content dynamically that includes background-color: #ffffff (or red) and showing it on a webpage, but the cell backgrounds appear as white in the P ...

Revamp the button's visual presentation when it is in an active state

Currently, I'm facing a challenge with altering the visual appearance of a button. Specifically, I want to make it resemble an arrow protruding from it, indicating that it is the active button. The button in question is enclosed within a card componen ...

Creating a CSS layout with tabs that include a visually appealing right space

My webpage is set up for tabbed browsing, with the tabs displayed as <li>s in block form. The parent div containing the tabs is floated to the right. However, I am encountering an issue where the last tab is not fully aligning with the right side of ...

Move the dropdown selection above all DIV elements

Check out this link: Make sure to select an option from the dropdown menu. Also, some of the options are hidden behind other elements. I noticed that if I target .join-form and remove overflow: hidden, the layout of the page becomes distorted. Can anyon ...

Discrepancies in button padding across desktop and mobile platforms

I have created a sample button and included a jsfiddle link to showcase the code. Due to the font properties of the specific font I'm using, I had to adjust the padding values differently for the top and bottom of the text to achieve a vertically cent ...

Problem arising from implementing background and relative positioning in html and css

Encountering an issue with html and css. Today, attempted to create a page with a large background image for the first time. Utilizing the foundation framework, but facing a challenge. Foundation is an adaptive framework, and when resizing the browser, a ...

Hide the 'white' color on visited links and category tags in the WordPress blog page

I created a custom WordPress theme, and I am having trouble styling the bootstrap navigation. I want the text to be white and change to brown when selected. However, my attempts to add a separate class for H1 tags on the blog post page have not been succes ...

What is the optimal resolution for a background image in a Cordova app?

Currently working on a Cordova application that requires different background images for various screens. Despite using media queries to provide the background image in different resolutions, we are facing an issue where the background image is not displ ...

What are the steps to vertically aligning rows in bootstrap?

I've been exploring different solutions to vertically center my rows within the container. Adding d-flex and align-items-center did work for centering them vertically but caused the rows to appear side by side. I'm curious if there's an alte ...