Ajax.ActionLink using an image instead of text
I've been working on setting up some Ajax.ActionLinks in my project. I can get the ActionLinks working 100%, however, my problem comes when I want to use an image instead of text for the link.
I've used a couple other StackOverflow posts and came up with the following solution to show an image inside an ActionLink:
Ajax.ActionLink("[replacethis]", "_Edit", "Account", new { id = "<#= UserId#>" }, new AjaxOptions() { UpdateTargetId = "subForm", InsertionMode = InsertionMode.Replace, HttpMethod = "GET" }).ToHtmlString().Replace("[replacethis]", "<img src=\"" + Url.Content("~/Content/images/page-pencil-24.png") + "\" />"))
When I use a normal Ajax.ActionLink, my HTML looks like this:
<a href="/ct/administration/Account/_Edit/be71fdfc-0427-46e4-8ce5-6fa7cdf639d3" data-ajax-update="#subForm" data-ajax-mode="replace" data-ajax-method="GET" data-ajax="true">Edit</a>
When I use my new method my HTML looks like this:
<a href="/ct/administration/Account/_Edit/be71fdfc-0427-46e4-8ce5-6fa7cdf639d3" data-ajax-update="#subForm" data-ajax-mode="replace" data-ajax-method="GET" data-ajax="true">
<img src="/ct/Content/images/page-pencil-24.png">
</a>
When I put a break point in my controller and check if Request.IsAjaxRequest() == true, it's always False for the ImageActionLink, but T开发者_如何学运维rue for the ActionLink.
Looks like a common problem - there's an answer here that might be useful - a custom HtmlHelper to do this. There's another here in VB.NET.
Edit:
Looks like IsAjaxRequest looks for an HTTP Header (X-Requested-With) being set to XMLHttpRequest. You can force this by passing a GET/POST variable, though it's a bit hacky:
http://www.britishdeveloper.co.uk/2010/10/aspnet-mvc-isajaxrequest-jquery.html
However, interestingly you can also set this as either part of your form POST or even a GET querystring! Such as www.example.com?x-requested-with=XMLHttpRequest (case sensitive). This will also make IsAjaxRequest() return true.
精彩评论