Attach image to ActionLink in MVC 4

前端 未结 4 1922
旧巷少年郎
旧巷少年郎 2021-01-14 01:33

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

 <         


        
相关标签:
4条回答
  • 2021-01-14 02:07

    The best way you can do for both Html.ActionLink and Ajax.ActionLink is as below

    @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\" … />"))
    

    additionaly you want to provide class and style so you can do as following

    @Html.Raw(@Ajax.ActionLink("[replacetext]", "Index", "Home", new AjaxOptions { UpdateTargetId="dvTest"}).ToHtmlString().Replace("[replacetext]", "<img src=\"/Contents/img/logo.png\" style=\"width:10%\" … />"))
    
    0 讨论(0)
  • 2021-01-14 02:11

    This is from my application. Works ok:

    .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>
    
    0 讨论(0)
  • 2021-01-14 02:11

    use your custom 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);
            }
        }
    }
    

    then in your view.cshtml:

    @using OrderPad2.Web.UI.Infrastructure
    .
    .
    .
    .
    @Html.ImageActionLink(@app.Icon,@app.AppStoreLink) 
    
    0 讨论(0)
  • 2021-01-14 02:19

    You only needed to change:

    new { id = "_logo"}
    

    to:

    new { @id = "_logo"} 
    

    Without the @ in front of id it treats it as a parameter instead of a attribute of the html element.

    Without the @, it would produce:

    www.site.com/home/index/_logo

    With the @, it would produce:

    www.site.com/home

    and the element would be:

    <a id="_logo" href=""></a>
    
    0 讨论(0)
提交回复
热议问题