4

I am using ActionLink with id in MVC 4 app and assinging actionLink id an image in css but on on earth I am doing wrong. is not working! here is my code

 <div class="logo_container">
            @Html.ActionLink(" ", "Index", "Home", null, new { id = "_Logo" })
 </div>

CSS

.logo_container {
width:339px;
height:116px; 
}

#_Logo {
background-image: url("../Images/logo.png");
 width:339px;
height:116px;
background-color:green;
}
4

4 回答 4

6

您可以为 Html.ActionLink 和 Ajax.ActionLink 执行的最佳方法如下

@Html.Raw(@Html.ActionLink("[replacetext]", "Index", "Home").ToHtmlString().Replace("[replacetext]", "<img src=\"/Content/img/logo.png\" ... />"))


@Html.Raw(@Ajax.ActionLink("[replacetext]", "Index", "Home", new AjaxOptions { UpdateTargetId="dvTest"}).ToHtmlString().Replace("[replacetext]", "<img src=\"/Contents/img/logo.png\" … />"))

另外,您想提供类和样式,因此您可以执行以下操作

@Html.Raw(@Ajax.ActionLink("[replacetext]", "Index", "Home", new AjaxOptions { UpdateTargetId="dvTest"}).ToHtmlString().Replace("[replacetext]", "<img src=\"/Contents/img/logo.png\" style=\"width:10%\" … />"))
于 2014-04-23T07:07:13.880 回答
6

这是来自我的应用程序。工作正常:

.logo{
background:no-repeat url(/Content/photo/png/sprite.png) 0 0;
height:15px;
width:20px;
overflow:hidden;
float:left;
border:none;
display:inline;
}

<a href="@Url.Action("Action", "Controller")" class="logo"></a>
于 2013-10-08T15:44:04.363 回答
5

使用您的自定义 HtmlHelper:

    namespace Order.Web.UI.Infrastructure
{
    public static class CustomHelpers
    {
        public static MvcHtmlString ImageActionLink(this HtmlHelper html, string imageSource, string url)
        {
            string imageActionLink = string.Format("<a href=\"{0},\"><img width=\"150\" height=\"150\" src=\"{1}\" /></a>",url,imageSource);

            return new MvcHtmlString(imageActionLink);
        }
    }
}

然后在您的 view.cshtml 中:

@using OrderPad2.Web.UI.Infrastructure
.
.
.
.
@Html.ImageActionLink(@app.Icon,@app.AppStoreLink) 
于 2014-02-24T16:54:11.627 回答
0

你只需要改变:

new { id = "_logo"}

至:

new { @id = "_logo"} 

如果没有@前面的,id它会将其视为参数而不是 html 元素的属性。

没有@它将产生:

www.site.com/home/index/_logo

使用@它将产生:

www.site.com/home

元素是:

<a id="_logo" href=""></a>
于 2014-02-11T21:22:16.317 回答