I am calling a controller/action from an action link to display the results in a partial view within bootstrap modal dialog. All is working as expected until the user requested the action link not to be text but to be an image.
I thought the best solution would be to create an actionlinkimage extension helper, which was based on the one Basheer posted as a solution on this question Html.ActionLink as a button or an image, not a link and it works brilliantly :
public static class ActionImageHelper
{
public static IHtmlString ImageActionLink(this HtmlHelper htmlHelper, string linkText, string action, string controller, object routeValues, object htmlAttributes, string imageSrc)
{
var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
var img = new TagBuilder("img");
img.Attributes.Add("src", VirtualPathUtility.ToAbsolute(imageSrc));
var anchor = new TagBuilder("a") { InnerHtml = img.ToString(TagRenderMode.SelfClosing) };
anchor.Attributes["href"] = urlHelper.Action(action, controller, routeValues);
anchor.MergeAttributes(new RouteValueDictionary(htmlAttributes));
return MvcHtmlString.Create(anchor.ToString());
}
}
However switching the standard @HtmlAction link to this extension helper i now cannot access the text attribute of the link in javascript , so when I attempt to extract the text to display as my dialog title it comes in blank , see javascript here :
<script type="text/javascript">
$(document).ready(function () {
$("body").on("click", "a.dialog-window", null, function (e) {
e.preventDefault();
var $link = $(this);
var title = $link.text(); //***Using the text prop for the title
$('#myModal .modal-title').html(title);
var url = $(this).attr('href');
if (url.indexOf('#') == 0) {
$('#myModal').modal('show');
}
else {
$.get(url, function (data) {
$('#myModal .te').html(data);
$('#myModal').modal();
});
}
});
});
</script>
As you can see on the 6th line , where I attempt to get the text from the link. This now comes back as empty. I have attempted to add the text property in the extension method but with no luck. Any help would be appreciated.
Using: jquery3.1.1 Bootstrap v3.3.7 .NET 4.5