如何把span元素ActionLink的MVC3里面呢? [英] How to put span element inside ActionLink MVC3?
问题描述
如何能我把一个跨度元素中的ActionLink但不与URL.ACTION?
这
<立GT;<跨度>
@ Ajax.ActionLink(连结文字,ControllerName,新AjaxOptions
{
UpdateTargetId =格,
InsertionMode = InsertionMode.Replace,
列举HTTPMethod =GET,
LoadingElementId =进步 })
< / SPAN>< /李>
生成此:
<立GT;
<跨度>
< A HREF =/首页/ ControllerName数据AJAX更新=#长卷
数据AJAX模式=替换数据Ajax的方法=GET
数据AJAX加载=#进步数据AJAX =真正的>连结文字< / A>
< / SPAN>
< /李>
但我需要别的东西。如何创建一个自定义的MVC3 ActionLink的方法,该方法生成的输出:
<立GT;
< A HREF =/首页/ ControllerName数据AJAX更新=#长卷
数据AJAX模式=替换数据Ajax的方法=GET
数据AJAX加载=#进步数据AJAX =真正的> <跨度>连结文字< / SPAN> //里面&LT生成此跨度; A> &所述; / A>
< /李>
如何把跨度元素中的ActionLink但不与URL.ACTION
块引用>答案很简单:你不能。该ActionLink的方法HTML EN codeS链接文本和没有多少,你可以做些什么(你可以打开微软的一票,让他们提供过载,使您可以为此在ASP.NET MVC vNext)。
目前,你可以写一个自定义的HTML帮助不会带code:
公共静态类AjaxExtensions
{
公共静态IHtmlString MyActionLink(
这AjaxHelper AjaxHelper,它
串LINKTEXT,
串actionName,
AjaxOptions ajaxOptions
)
{
VAR targetUrl这个= UrlHelper.GenerateUrl(NULL,actionName,NULL,NULL,ajaxHelper.RouteCollection,ajaxHelper.ViewContext.RequestContext,真正的);
返回MvcHtmlString.Create(ajaxHelper.GenerateLink(LINKTEXT,targetUrl这个,ajaxOptions ??新AjaxOptions(),NULL));
} 私人静态字符串GenerateLink(
这AjaxHelper AjaxHelper,它
串LINKTEXT,
串targetUrl这个,
AjaxOptions ajaxOptions,
IDictionary的<字符串对象> htmlAttributes
)
{
变种一个=新TagBuilder(一)
{
的innerHTML = LINKTEXT
};
a.MergeAttributes<字符串对象>(htmlAttributes);
a.MergeAttribute(的href,targetUrl这个);
a.MergeAttributes&所述;串,对象>(ajaxOptions.ToUnobtrusiveHtmlAttributes());
返回a.ToString(TagRenderMode.Normal);
}
}和则:
@ Ajax.MyActionLink(
&所述;跨度>连结文字及下; /跨度>中,
ActionName
新AjaxOptions {
UpdateTargetId =格,
InsertionMode = InsertionMode.Replace,
列举HTTPMethod =GET,
LoadingElementId =进步
}
)How can I put a span element inside ActionLink BUT NOT WITH URL.ACTION?
This:
<li><span> @Ajax.ActionLink("LinkText", "ControllerName", new AjaxOptions { UpdateTargetId = "div", InsertionMode = InsertionMode.Replace, HttpMethod = "GET", LoadingElementId = "progress" }) </span></li>
generates this:
<li> <span> <a href="/Home/ControllerName" data-ajax-update="#scroll" data-ajax-mode="replace" data-ajax-method="GET" data-ajax-loading="#progress" data-ajax="true">LinkText</a> </span> </li>
But I need something else. How can I create a custom MVC3 ActionLink method that generates this output:
<li> <a href="/Home/ControllerName" data-ajax-update="#scroll" data-ajax-mode="replace" data-ajax-method="GET" data-ajax-loading="#progress" data-ajax="true"> <span>LinkText</span> // this span generated inside <a> </a> </li>
解决方案How to put span element inside ActionLink BUT NOT WITH URL.ACTION
Simple answer: you can't. The ActionLink method HTML encodes the link text and there's not much you could do about it (you could open a ticket at Microsoft so that they provide an overload that allows you to do this in ASP.NET MVC vNext).
At the moment you could write a custom html helper that won't encode:
public static class AjaxExtensions { public static IHtmlString MyActionLink( this AjaxHelper ajaxHelper, string linkText, string actionName, AjaxOptions ajaxOptions ) { var targetUrl = UrlHelper.GenerateUrl(null, actionName, null, null, ajaxHelper.RouteCollection, ajaxHelper.ViewContext.RequestContext, true); return MvcHtmlString.Create(ajaxHelper.GenerateLink(linkText, targetUrl, ajaxOptions ?? new AjaxOptions(), null)); } private static string GenerateLink( this AjaxHelper ajaxHelper, string linkText, string targetUrl, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes ) { var a = new TagBuilder("a") { InnerHtml = linkText }; a.MergeAttributes<string, object>(htmlAttributes); a.MergeAttribute("href", targetUrl); a.MergeAttributes<string, object>(ajaxOptions.ToUnobtrusiveHtmlAttributes()); return a.ToString(TagRenderMode.Normal); } }
and then:
@Ajax.MyActionLink( "<span>LinkText</span>", "ActionName", new AjaxOptions { UpdateTargetId = "div", InsertionMode = InsertionMode.Replace, HttpMethod = "GET", LoadingElementId = "progress" } )
这篇关于如何把span元素ActionLink的MVC3里面呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!