画像相当のActionLinkにASP.NET MVC
-
03-07-2019 - |
質問
にASP.NET MVCはありました。ActionLinkヘルパーのためのImgタグ?
私にはコントローラのアクションを出力する動的に生成されJPEG、たいと思った同一のラムダ仕様の表現へのリンクをしていHref用ActionLink.
また、ヘルパーでは、URLへのルートものを使用して指定されLambdasうすることも可能です。
編集:また特定した私が使っていた5プレビューしていたように、これまでとは違った、ベータを公開しました。いすべてのバージョン番号だった不要な曲情報のみをどう高度化すぐに:-)
解決
ます。アクション()を素URLほとんどの過負荷のHtml.ActionLinkいていますが、私のURLからのラムダの機能はじます。ActionLinkいます。ばれん追加類似の過負荷へのUrl。行動はいないのです。
他のヒント
で利用できます。行動方法
<a href="<%= Url.Action("Create") %>"><img src="../../Content/Images/add_48.png" /></a>
この質問は、以前のってASP.NET MVC時にRCの一方で既存の""についても中に入ると広々とした明るいエこの質問以降の私にはこの面白いかもしれませ:
少なくとも、RCに使用できます。アクション()また匿名のタイプには、結果に見え念の提案が上かもしれません:
<a href="<%= Url.RouteUrl("MyRoute", new { param1 = "bla", param2 = 5 }) %>">
put in <span>whatever</span> you want, also <img src="a.gif" alt="images" />.
</a>
その他の多くの過負荷のためのRouteUrlします。
使用した回避策をマーカーの代わりにテキストActionLink、その後で私のイメージコードです。のようなこと:
<%= Html.ActionLink("__IMAGE_PLACEHOLDER__", "Products").Replace("__IMAGE_PLACEHOLDER__", "<img src=\"" + myImgUrl + "\" />")%>
ない最もエレガントな解決方法です。
にMVC3、リンクのようになります:
<a href="@Url.Action("Create")"><img src="../../Content/Images/add_48.png" /></a>
にASP.NET MVCベータを使用できます Html.BuildUrlFromExpression 方法は、先物取引議に含まれないデフォルトのASP.NET MVCインストールが可能 CodePlex)当ウェブサイトへのリンクを周りをイメージはHTMLを使用のムダ-スタイルActionLink書式では、このように:
<a href="<%=Html.BuildUrlFromExpression<MyController>(c => c.MyAction())%>">
<%=Html.Image("~/Content/MyImage.gif")%>
</a>
自画像リンクのボーダレス化する必要があります追加CSS規則のようになります:
img
{
border: none;
}
利用することができます。このように動作すActionLink.
http://agilefutures.com/index.php/2009/06/actionimage-aspnet-mvc
このイラストはピアプロの達成にMVC2.って自分の非常に簡単な拡張手法の支援がラムダ仕様の表現のUrl。行動をするヘルパーいす必要があるので参考MVC2スピーチでした。
こちらのコード:
using System;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Routing;
using ExpressionHelperInternal=Microsoft.Web.Mvc.Internal.ExpressionHelper;
namespace Bnv.Bssi.Web.Infrastructure.Helpers
{
public static class UrlExtensions
{
public static string Action<TController>(this UrlHelper helper, Expression<Action<TController>> action) where TController : Controller
{
RouteValueDictionary routeValuesFromExpression = ExpressionHelperInternal.GetRouteValuesFromExpression<TController>(action);
return helper.Action(routeValuesFromExpression["action"].ToString(), routeValuesFromExpression);
}
}
}
これはどのように使用すると:
<img src="<%= Url.Action<YourController>(c => c.YourActionMethod(param1, param2)); %>" />
もしかすると後でもがんのシェア:)
私は新規追加した拡張法のようなこと:
public static class ImageExtensions
{
public static MvcHtmlString ImageLink(this HtmlHelper htmlHelper, string imgSrc, string additionalText = null, string actionName = null, string controllerName = null, object routeValues = null, object linkHtmlAttributes = null, object imgHtmlAttributes = null)
{
var urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url;
var url = "#";
if (!string.IsNullOrEmpty(actionName))
url = urlHelper.Action(actionName, controllerName, routeValues);
var imglink = new TagBuilder("a");
imglink.MergeAttribute("href", url);
imglink.InnerHtml = htmlHelper.Image(imgSrc, imgHtmlAttributes) + " " + additionalText;
linkHtmlAttributes = new RouteValueDictionary(linkHtmlAttributes);
imglink.MergeAttributes((IDictionary<string, object>)linkHtmlAttributes, true);
return MvcHtmlString.Create(imglink.ToString());
}
public static MvcHtmlString Image(this HtmlHelper htmlHelper, string imgSrc, object imgHtmlAttributes = null)
{
var imgTag = new TagBuilder("img");
imgTag.MergeAttribute("src", imgSrc);
if (imgHtmlAttributes != null)
{
imgHtmlAttributes = new RouteValueDictionary(imgHtmlAttributes);
imgTag.MergeAttributes((IDictionary<string, object>)imgHtmlAttributes, true);
}
return MvcHtmlString.Create(imgTag.ToString());
}
}
これを実施したと日経が報じている。
はます。コンテンツ(うのです。
このようなもの。コンテンツ("~/path/to/something.jpg")になりますので、適切なパスに基づく申請です。
-ジ
また、上記の応答とした"というメールが来たのラッパーの拡張:
public static MvcHtmlString ActionImageLink(this HtmlHelper helper, string src, string altText, UrlHelper url, string actionName, string controllerName)
{
return ActionImageLink(helper, src, altText, url, actionName, controllerName, null, null);
}
public static MvcHtmlString ActionImageLink(this HtmlHelper helper, string src, string altText, UrlHelper url, string actionName, string controllerName, Dictionary<string, string> linkAttributes, Dictionary<string, string> imageAttributes)
{
return ActionImageLink(helper, src, altText, url, actionName, controllerName, null, linkAttributes, imageAttributes);
}
public static MvcHtmlString ActionImageLink(this HtmlHelper helper, string src, string altText, UrlHelper url, string actionName, string controllerName, dynamic routeValues, Dictionary<string, string> linkAttributes, Dictionary<string, string> imageAttributes)
{
var linkBuilder = new TagBuilder("a");
linkBuilder.MergeAttribute("href", routeValues == null ? url.Action(actionName, controllerName) : url.Action(actionName, controllerName, routeValues));
var imageBuilder = new TagBuilder("img");
imageBuilder.MergeAttribute("src", url.Content(src));
imageBuilder.MergeAttribute("alt", altText);
if (linkAttributes != null)
{
foreach (KeyValuePair<string, string> attribute in linkAttributes)
{
if (!string.IsNullOrWhiteSpace(attribute.Key) && !string.IsNullOrWhiteSpace(attribute.Value))
{
linkBuilder.MergeAttribute(attribute.Key, attribute.Value);
}
}
}
if (imageAttributes != null)
{
foreach (KeyValuePair<string, string> attribute in imageAttributes)
{
if (!string.IsNullOrWhiteSpace(attribute.Key) && !string.IsNullOrWhiteSpace(attribute.Value))
{
imageBuilder.MergeAttribute(attribute.Key, attribute.Value);
}
}
}
linkBuilder.InnerHtml = MvcHtmlString.Create(imageBuilder.ToString(TagRenderMode.SelfClosing)).ToString();
return MvcHtmlString.Create(linkBuilder.ToString());
}
やすくなった私にとってとにかく、希望するから。
また、出力の Html.画像 私には Html.ImageLink するヘルパー
@(new HtmlString(Html.ActionLink(Html.Image("image.gif").ToString(), "myAction", "MyController").ToString().Replace("<", "<").Replace(">", ">")))
の問題というのは、ActionLink名の符号化されない<く <.
たばこのエンコーディングの結果の作品でした。(はいよいということではなく使用の交換?)
追加のポス:私の場合(asp.net mvc3)たかったので画像へのリンク行為としての言語のセレクタのようになってい:
public static MvcHtmlString ImageLink(this HtmlHelper htmlHelper, string imgSrc, string cultureName, object htmlAttributes, object imgHtmlAttributes, string languageRouteName = "lang", bool strictSelected = false)
{
UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url;
TagBuilder imgTag = new TagBuilder("img");
imgTag.MergeAttribute("src", imgSrc);
imgTag.MergeAttributes((IDictionary<string, string>)imgHtmlAttributes, true);
var language = htmlHelper.LanguageUrl(cultureName, languageRouteName, strictSelected);
string url = language.Url;
TagBuilder imglink = new TagBuilder("a");
imglink.MergeAttribute("href", url);
imglink.InnerHtml = imgTag.ToString();
imglink.MergeAttributes((IDictionary<string, string>)htmlAttributes, true);
//if the current page already contains the language parameter make sure the corresponding html element is marked
string currentLanguage = htmlHelper.ViewContext.RouteData.GetRequiredString("lang");
if (cultureName.Equals(currentLanguage, StringComparison.InvariantCultureIgnoreCase))
{
imglink.AddCssClass("selectedLanguage");
}
return new MvcHtmlString(imglink.ToString());
}
内部化したのに対して、言語のルートオリジナルソース こちらの.
素敵な解決が何をしたい場合にて、キャラクター設定をイメージをactionlink?こうしてい:
@using (Html.BeginForm("Action", "Controler", ajaxOptions))
{
<button type="submit">
<img src="image.png" />
</button>
}
の欠点ではまだまだビットを行うスタイリングのボタンに要素がすべてのhtmlいます。
とで、Ajaxのヘルパーなどの https://stackoverflow.com/a/19302438/961139