Search code examples
cssimagehtml.actionlink

Css Image For 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;" }));

Hi,

I try to set image for actionlink however it is always empty.

How can i set image to html.actionlink ?

I want actionlink to appear like image(image button)

Note : Please do not offer me a href etc.

Any help will be greatly appreciated.

Thanks.


Solution

  • As mentioned in comments, your image doesn't display when you aren't providing any text as your element is set to display as inline. With no text, your element has no size, regardless of any width or height properties that are specified in the element's style. To fix this, set 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;
            "
        })
    );