링크 텍스트에서 html 요소로 ActionLink 생성
-
03-07-2019 - |
문제
ASP.NET MVC보기에는 양식의 링크를 포함하고 싶습니다.
<a href="blah">Link text <span>with further descriptive text</span></a>
포함하려고합니다 <span>
요소 linkText
전화의 분야 Html.ActionLink()
예상대로 인코딩되는 것으로 끝납니다.
이것을 달성하는 권장 방법이 있습니까?
해결책
url.action을 사용하여 링크를 작성할 수 있습니다.
<a href="<% =Url.Action("Action", "Controller")%>">link text <span>with further blablah</span></a>
또는 html.buildurlpromexpression 사용 :
<a href="<% =Html.BuildUrlFromExpression<Controller>(c => c.Action()) %>">text <span>text</span></a>
다른 팁
면도기를 사용하는 것을 좋아한다면 이것이 작동해야합니다.
<a href="@Url.Action("Action", "Controller")">link text <span>with further blablah</span></a>
또 다른 옵션은 html.actionlink 또는 ajax.actionlink (컨텍스트에 따라)를 사용하여 평범한 MVCHTMLString에 액션 링크를 렌더링 한 다음 렌더링 된 MVCHTMLSTRING을 취하고 HTML 링크 텍스트를 직접 해킹하는 클래스를 작성하는 것입니다. 이미 mvchtmlstring을 렌더링하고 다른 mvchtmlstring을 반환합니다.
따라서 이것은 다음과 같은 클래스입니다.
namespace Bonk.Framework
{
public class CustomHTML
{
static public MvcHtmlString AddLinkText(MvcHtmlString htmlString, string linkText)
{
string raw = htmlString.ToString();
string left = raw.Substring(0, raw.IndexOf(">") + 1);
string right = raw.Substring(raw.LastIndexOf("<"));
string composed = left + linkText + right;
return new MvcHtmlString(composed);
}
}
}
그런 다음 다음과 같은 시야에서 사용합니다.
@Bonk.Framework.CustomHTML.AddLinkText(Ajax.ActionLink("text to be replaced", "DeleteNotificationCorporateRecipient"), @"Link text <span>with further descriptive text</span>")
이 접근법은 태그 렌더링 프로세스를 재현/이해하지 않아도된다는 이점이 있습니다.
제휴하지 않습니다 StackOverflow