C# 图片按钮Ajax.ActionLink
我正在尝试创建C# 图片按钮Ajax.ActionLink,c#,ajax,asp.net-mvc,C#,Ajax,Asp.net Mvc,我正在尝试创建Ajax.ActionLink的扩展。我对Html.ActionLink及其工作原理也做了同样的处理,但当我尝试对Ajax方法进行扩展时,它不起作用。发生的情况是,当我单击链接时,它不会呈现我的局部视图(替换),而是将我重定向到该视图。我想指出的是,Ajax.ActionLink方法运行良好 这是我的密码: public static IHtmlString ImageActionLink(this AjaxHelper ajaxHelper, string linkText, s
Ajax.ActionLink
的扩展。我对Html.ActionLink
及其工作原理也做了同样的处理,但当我尝试对Ajax方法进行扩展时,它不起作用。发生的情况是,当我单击链接时,它不会呈现我的局部视图(替换),而是将我重定向到该视图。我想指出的是,Ajax.ActionLink方法运行良好
这是我的密码:
public static IHtmlString ImageActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName,
object routeValues, object linkHtmlAttributes, AjaxOptions ajaxOptions, string imageSrc, object imageHtmlAttributes)
{
UrlHelper urlHelper = new UrlHelper(ajaxHelper.ViewContext.RequestContext);
//Image tag
TagBuilder imageTag = new TagBuilder("img");
imageTag.Attributes.Add("src", VirtualPathUtility.ToAbsolute(imageSrc));
imageTag.MergeAttributes(new RouteValueDictionary(imageHtmlAttributes), false);
//Anchor tag
TagBuilder anchorTag = new TagBuilder("a");
anchorTag.InnerHtml = imageTag.ToString(TagRenderMode.SelfClosing);
anchorTag.Attributes.Add("href", urlHelper.Action(actionName, controllerName, routeValues));
anchorTag.MergeAttributes(new RouteValueDictionary(ajaxOptions), false);
anchorTag.MergeAttributes(new RouteValueDictionary(linkHtmlAttributes), false);
return MvcHtmlString.Create(anchorTag.ToString());
}
我已尝试调试该问题,因此比较了从普通方法和扩展方法生成的Html:
正常(工作):
分机:
<a allowcache="False" confirm="" httpmethod="" insertionmode="Replace" loadingelementduration="0" loadingelementid="" onbegin="" oncomplete="" onfailure="" onsuccess="" updatetargetid="Edit" url="" href="/Admin/Event/Edit/1"><img src="/Content/Images/edit-icon.png"></a>
呈现的Html是不同的,因为普通方法使用Html
data-*
属性,而另一种方法不使用。我不知道如何解决这个问题。您需要使用AjaxOptions
的.ToUnobtrusiveHtmlAttributes()
方法来生成正确的数据ajax-*
属性。你的方法应该是
public static IHtmlString ImageActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName,
object routeValues, object linkHtmlAttributes, AjaxOptions ajaxOptions, string imageSrc, object imageHtmlAttributes)
{
UrlHelper urlHelper = new UrlHelper(ajaxHelper.ViewContext.RequestContext);
//Image tag
TagBuilder imageTag = new TagBuilder("img");
imageTag.Attributes.Add("src", VirtualPathUtility.ToAbsolute(imageSrc));
imageTag.MergeAttributes(new RouteValueDictionary(imageHtmlAttributes), false);
//Anchor tag
TagBuilder anchorTag = new TagBuilder("a");
anchorTag.InnerHtml = imageTag.ToString(TagRenderMode.SelfClosing);
anchorTag.Attributes.Add("href", urlHelper.Action(actionName, controllerName, routeValues));
// change the following line
anchorTag.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes());
// recommend the following change
anchorTag.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(linkHtmlAttributes)));
return MvcHtmlString.Create(anchorTag.ToString());
}
旁注:请参阅.ToUnobtrusiveHtmlAttributes()
方法
public static IHtmlString ImageActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName,
object routeValues, object linkHtmlAttributes, AjaxOptions ajaxOptions, string imageSrc, object imageHtmlAttributes)
{
UrlHelper urlHelper = new UrlHelper(ajaxHelper.ViewContext.RequestContext);
//Image tag
TagBuilder imageTag = new TagBuilder("img");
imageTag.Attributes.Add("src", VirtualPathUtility.ToAbsolute(imageSrc));
imageTag.MergeAttributes(new RouteValueDictionary(imageHtmlAttributes), false);
//Anchor tag
TagBuilder anchorTag = new TagBuilder("a");
anchorTag.InnerHtml = imageTag.ToString(TagRenderMode.SelfClosing);
anchorTag.Attributes.Add("href", urlHelper.Action(actionName, controllerName, routeValues));
// change the following line
anchorTag.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes());
// recommend the following change
anchorTag.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(linkHtmlAttributes)));
return MvcHtmlString.Create(anchorTag.ToString());
}