自定义AjaxHelper扩展,合并AjaxOptions自定义、AjaxHelper、AjaxOptions

2023-09-10 17:57:22 作者:一个人也不错

我建设,以创建一个Ajax.ActionImage(...)方法(见下文code)客户ajaxhelper扩展。

我不知道什么如何做的是合并的AjaxOptions到我的锚点href属性。我可以使用ajax.ActionLink(...),但是我不知道吨如何构建创建MvcHtmlString在我的图像元素。

在此先感谢!

 <扩展()> _
    公共职能ActionImage(BYVAL阿贾克斯AjaxHelper,它BYVAL控制作为字符串,BYVAL动作作为字符串,BYVAL routeValues​​作为对象,BYVAL AjaxOptions作为对象,BYVAL的ImagePath作为字符串,BYVAL AL​​T作为字符串,BYVAL宽度为整数,BYVAL高度为整数)作为MvcHtmlString

        昏暗的URL =新UrlHelper(ajax.ViewContext.RequestContext)

        昏暗imgHtml作为字符串
        昏暗anchorHtml作为字符串
        昏暗的imgbuilder =新TagBuilder(IMG)

        imgbuilder.MergeAttribute(src用户,url.Content(的ImagePath))
        imgbuilder.MergeAttribute(Alt键,ALT)
        imgbuilder.MergeAttribute(宽度,宽)
        imgbuilder.MergeAttribute(高度,高度)
        imgHtml = imgbuilder.ToString(TagRenderMode.SelfClosing)

        昏暗anchorBuilder =新TagBuilder(一)
        anchorBuilder.MergeAttribute(HREF,url.Action(动作,控制器,routeValues​​))
        anchorBuilder.InnerHtml = imgHtml
        anchorHtml = anchorBuilder.ToString(TagRenderMode.Normal)

        返回MvcHtmlString.Create(anchorHtml)

    端功能
 

解决方案

首先,你需要改变变量ajaxOptions到AjaxOptions的类型(在System.Web.Ajax命名空间)。一旦你做到了这一点,你可以添加以下到您的ajaxOptions合并到锚标记:

 如果ajaxHelper.ViewContext.UnobtrusiveJavaScriptEnabled然后
    anchorBuilder.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes())
结束如果
 

您不希望您的href里面的选项。该选项必须是锚标记的一部分,才能被正确地jquery.unobtrusive-ajax.js解析。

counsellorben

谷歌服务助手2021插件下载 谷歌服务助手2021 PC最新版下载V1.19.7

I am building a customer ajaxhelper extension in order to create an Ajax.ActionImage(...) method (see code below).

What I don't know how to do is "merge" the AjaxOptions into my anchor href attribute. I could use ajax.ActionLink(...) but then I don't know how t build my image element inside the created MvcHtmlString.

Thanks in advance!

    <Extension()> _
    Public Function ActionImage(ByVal ajax As AjaxHelper, ByVal controller As String, ByVal action As String, ByVal routeValues As Object, ByVal AjaxOptions As Object, ByVal imagePath As String, ByVal alt As String, ByVal width As Integer, ByVal height As Integer) As MvcHtmlString

        Dim url = New UrlHelper(ajax.ViewContext.RequestContext)

        Dim imgHtml As String
        Dim anchorHtml As String
        Dim imgbuilder = New TagBuilder("img")

        imgbuilder.MergeAttribute("src", url.Content(imagePath))
        imgbuilder.MergeAttribute("alt", alt)
        imgbuilder.MergeAttribute("width", width)
        imgbuilder.MergeAttribute("height", height)
        imgHtml = imgbuilder.ToString(TagRenderMode.SelfClosing)

        Dim anchorBuilder = New TagBuilder("a")
        anchorBuilder.MergeAttribute("href", url.Action(action, controller, routeValues))
        anchorBuilder.InnerHtml = imgHtml
        anchorHtml = anchorBuilder.ToString(TagRenderMode.Normal)

        Return MvcHtmlString.Create(anchorHtml)

    End Function

解决方案

First, you need to change the type of the variable ajaxOptions to AjaxOptions (in the System.Web.Ajax namespace). Once you have done this, you can add the following to merge your ajaxOptions into your anchor tag:

If ajaxHelper.ViewContext.UnobtrusiveJavaScriptEnabled Then
    anchorBuilder.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes())
End If

You do not want the options inside your href. The options must be part of the anchor tag, in order to be parsed correctly by jquery.unobtrusive-ajax.js.

counsellorben