将更多路由值添加到自定义静态html帮助程序

时间:2017-08-22 14:28:38

标签: asp.net-mvc html-helper

我有以下问题 我创建了自定义帮助程序,在单击时返回部分视图。

class helper controller

字符串" routevalues"只接受一个字符串(在本例中为property.Part.number)并将其发送到控制器。 控制器将自动用我发送的值填充字符串id。 我希望能够发送多个字符串。 示例:id =" 123",foo =" bar"。

  @Ajax.ImageActionLink("/InternalDB/img/box/" + @property.Part.part_number + ".jpg",                            
                           "popup",
                            "85px",
                            "65px",
                            "PartialViewImageRotation",                                
                             property.Part.part_number,
                                 new AjaxOptions
                                 {
                                     UpdateTargetId = "ImageRotation",
                                     InsertionMode = InsertionMode.Replace,
                                     HttpMethod = "GET"
                                 })


    public static IHtmlString ImageActionLink(this AjaxHelper helper, string imageUrl, string altText,string width, string height, string actionName, 
        string routeValues, AjaxOptions ajaxOptions)
    {
        var builder = new TagBuilder("img");
        builder.MergeAttribute("src", imageUrl);
        builder.MergeAttribute("alt", altText);
        builder.MergeAttribute("width", width);
        builder.MergeAttribute("height", height);
        var link = helper.ActionLink("[replaceme]", actionName+"/"+routeValues, ajaxOptions).ToHtmlString();
        return MvcHtmlString.Create(link.Replace("[replaceme]", builder.ToString(TagRenderMode.SelfClosing)));
    }



    public ActionResult PartialViewImageRotation(string routeValues,string id, string ImgWidthHTML5Viewer, string ImgHeightHTML5Viewer,string ImgWidthFlashPlayer,
        string ImgHeightFlashPlayer, string ImgFloatPosition ,string ImgDivWidthRotatePlayer,  string ImgDivPadding, string ImgWidthRotatePlayer, string ImgHeightRotatePlayer)
    {
        ViewBag.PartNumber = id;

        ViewBag.ImgWidthHTML5Viewer = ImgWidthHTML5Viewer;
        ViewBag.ImgHeightHTML5Viewer = ImgHeightHTML5Viewer;

        ViewBag.ImgWidthFlashPlayer = ImgWidthFlashPlayer;
        ViewBag.ImgHeightFlashPlayer = ImgHeightFlashPlayer;
        ViewBag.ImgFloatPosition = ImgFloatPosition;

        ViewBag.ImgDivWidthRotatePlayer = ImgDivWidthRotatePlayer;
        ViewBag.ImgDivPadding = ImgDivPadding;

        ViewBag.ImgWidthRotatePlayer = ImgWidthRotatePlayer;
        ViewBag.ImgHeightRotatePlayer = ImgHeightRotatePlayer;

        var firstImageRelativePath = "~/img/HTML/" + id + "/Profile.xml";
        var firstImageAbsolutePath = HttpContext.Server.MapPath(firstImageRelativePath);
        ViewBag.FirstImageCheck = System.IO.File.Exists(firstImageAbsolutePath);
        //possible problem : "\\" are doubled in the absolute path 

        var secondImageRelativePath = "~/img/SWF/" + id + "/" + id + ".swf";
        var secondImageAbsolutePath = HttpContext.Server.MapPath(secondImageRelativePath);
        ViewBag.SecondImageCheck = System.IO.File.Exists(secondImageAbsolutePath);

        return PartialView();

1 个答案:

答案 0 :(得分:0)

我找到了解决这个问题的方法。我需要做的就是改变一些事情

public static IHtmlString ImageActionLink(this AjaxHelper helper, string imageUrl, string altText,string width, string height, string actionName, object routeValues, AjaxOptions ajaxOptions) { var builder = new TagBuilder("img"); builder.MergeAttribute("src", imageUrl); builder.MergeAttribute("alt", altText); builder.MergeAttribute("width", width); builder.MergeAttribute("height", height); var link = helper.ActionLink("[replaceme]", actionName, routeValues, ajaxOptions).ToHtmlString(); return MvcHtmlString.Create(link.Replace("[replaceme]", builder.ToString(TagRenderMode.SelfClosing))); }

现在我可以发送多个值。

相关问题