与@ Ajax.ActionLink动态图像和格式化参数图像、参数、动态、Ajax

2023-09-10 18:16:17 作者:蹦擦蹦擦蹦擦擦

我试图更改以下工作code到阿贾克斯。

 < A HREF =@ Url.Action(指数,新{PN = PC})>
   < IMG SRC =@ Url.Content(〜/图片/+ @photoFile [PC]),ID =imgnbWIDTH =100px的高度=150像素ALT =照片/>&LT ; / a取代; < BR />
 

在code需要处理字符串[]变量,@photoFile [PC]),并采取格式化(ID =imgnbWIDTH =100px的高度=150像素ALT =照片) 。 (imgnb是CSS,没有图像边框)

梭萌建立了一个AJAX辅助这里Problem与ajax.actionlink帮手,返回字符串。

他的code的伟大工程与没有paratmeters静态图像。

  @ Ajax.ImageActionLink(../../照片/儿童/ albersona1.jpg,索引,新{PN = PC},新AjaxOptions
    {
      UpdateTargetId =选择缩略图,
      InsertionMode = InsertionMode.Replace,
      HttpMethod =GET})
 

请问谁知道如何修改这个助手的URL.Action转化为阿贾克斯,使其处理动态图像和格式参数。

任何帮助非常AP preciated。

谢谢,乔

解决方案

 公共静态类ImageActionLinkHelper
{
    公共静态IHtmlString ImageActionLink(
        这AjaxHelper帮手,
        串IMAGEURL,
        串actionName,
        对象routeValues​​,
        对象htmlAttributes,
        AjaxOptions ajaxOptions
    )
    {
        VAR建设者=新TagBuilder(IMG);
        builder.MergeAttribute(src用户,IMAGEURL);
        builder.MergeAttributes(新RouteValueDictionary(htmlAttributes));
        VAR链接= helper.ActionLink([replaceme],actionName,routeValues​​,ajaxOptions);
        。VAR HTML = link.ToHtmlString()更换([replaceme],builder.ToString(TagRenderMode.SelfClosing));
        返回新HtmlString(HTML);
    }
}
 
Ajax数据采集界面预览 Ajax数据采集界面图片

然后:

  @ Ajax.ImageActionLink(
    Url.Content(〜/图片/+ photoFile [PC]),
    指数,
    新{PN = PC},
    新{ID =imgnbWIDTH =100像素,高度=150像素,ALT =照片},
    新AjaxOptions
    {
        UpdateTargetId =富
    }
)
 

I’m trying to change the following working code into Ajax.

   <a href="@Url.Action("Index", new { pn = pc })"> 
   <img src="@Url.Content("~/Photos/" + @photoFile[pc])", id = "imgnb" width = "100px" height = "150px" alt = "Photo" /></a>  <br />

The code needs to handle a string[] variable, @photoFile[pc]), and take formatting, ( id = "imgnb" width = "100px" height = "150px" alt = "Photo"). (imgnb is css, no image border)

Soe Moe built an Ajax helper here Problem with ajax.actionlink helper, return string.

His code works great with a static image with no paratmeters.

         @Ajax.ImageActionLink("../../Photos/Children/albersona1.jpg", "Index", new { pn = pc }, new AjaxOptions
    {
      UpdateTargetId = "Selected Thumbnail",
      InsertionMode = InsertionMode.Replace,
      HttpMethod = "GET" })

Would anyone know how to modify this helper to convert the URL.Action into Ajax so that it handles dynamic images and formatting parameters.

Any help greatly appreciated.

Thanks, Joe

解决方案

public static class ImageActionLinkHelper
{
    public static IHtmlString ImageActionLink(
        this AjaxHelper helper,
        string imageUrl,
        string actionName,
        object routeValues,
        object htmlAttributes,
        AjaxOptions ajaxOptions
    )
    {
        var builder = new TagBuilder("img");
        builder.MergeAttribute("src", imageUrl);
        builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
        var link = helper.ActionLink("[replaceme]", actionName, routeValues, ajaxOptions);
        var html = link.ToHtmlString().Replace("[replaceme]", builder.ToString(TagRenderMode.SelfClosing));
        return new HtmlString(html);
    }
}

and then:

@Ajax.ImageActionLink(
    Url.Content("~/Photos/" + photoFile[pc]),
    "Index",
    new { pn = pc },
    new { id = "imgnb", width = "100px", height = "150px", alt = "Photo" },
    new AjaxOptions
    {
        UpdateTargetId = "foo"
    }
)