Adding images within Html.ActionLink

前端 未结 4 2275
攒了一身酷
攒了一身酷 2021-02-18 21:02

I was trying to create an option to switch between a list view and widget view in ASP.net MVC (with razor view engine).

However, I am having some trouble trying to both

4条回答
  •  臣服心动
    2021-02-18 21:29

    The reason this code did not work:

    @Html.ActionLink("List View", "listView", new { @style = "background-image:url('~/Content/Images/listIcon.png')" },null)
    

    was because the 3rd parameter of @Html.ActionLink is to add additional route values. If you want to add more HTML attributes, you need to use:

    @Html.ActionLink("List View", "listView", null, new { @style = "background-image:url('~/Content/Images/listIcon.png')" })
    

    Additionally, like others have said, you can't use the ~.

    Note that inline styles are generally frowned upon, as the best practice would be to create a CSS class that contains your background-image and add the class as the HTML attribute instead, but @style would functionally work here as well. More info on why inline styles are bad can be found here: What's so bad about in-line CSS?

提交回复
热议问题