2017-01-11 85 views
0

我從操作鏈接調用控制器/操作以在引導模式對話框中的局部視圖中顯示結果。所有操作都按預期工作,直到用戶請求操作鏈接不是文本而是圖像。鏈接文本屬性在Action中不可用JavaScript鏈接擴展方法MVC4

我認爲最好的辦法是建立一個actionlinkimage擴展幫助,這是基於一個BASHEER張貼在這個問題上Html.ActionLink as a button or an image, not a link一個解決方案,它出色的作品:

public static class ActionImageHelper 
    { 

     public static IHtmlString ImageActionLink(this HtmlHelper htmlHelper, string linkText, string action, string controller, object routeValues, object htmlAttributes, string imageSrc) 
     { 
      var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext); 
      var img = new TagBuilder("img"); 
      img.Attributes.Add("src", VirtualPathUtility.ToAbsolute(imageSrc)); 
      var anchor = new TagBuilder("a") { InnerHtml = img.ToString(TagRenderMode.SelfClosing) }; 
      anchor.Attributes["href"] = urlHelper.Action(action, controller, routeValues); 
      anchor.MergeAttributes(new RouteValueDictionary(htmlAttributes)); 
      return MvcHtmlString.Create(anchor.ToString()); 

     } 

    } 

但是切換標準@ HtmlAction連結此擴展幫助我現在不能使用在JavaScript中的鏈接的文本屬性,所以,當我試圖提取要顯示的文字,因爲它涉及的空白我的對話框的標題,看到的JavaScript這裏:

<script type="text/javascript"> 

    $(document).ready(function() { 
     $("body").on("click", "a.dialog-window", null, function (e) { 

      e.preventDefault(); 

      var $link = $(this);   
      var title = $link.text(); //***Using the text prop for the title 
      $('#myModal .modal-title').html(title); 

      var url = $(this).attr('href'); 
      if (url.indexOf('#') == 0) { 
       $('#myModal').modal('show'); 
      } 
      else { 

       $.get(url, function (data) { 
        $('#myModal .te').html(data); 
        $('#myModal').modal(); 
       }); 

      } 

     }); 
    }); 

</script> 

正如您可以在第6行看到的那樣,我試圖從鏈接中獲取文本。現在這個回來是空的。 我試圖在擴展方法中添加文本屬性,但沒有運氣。任何幫助,將不勝感激。

使用: jquery3.1.1 引導v3.3.7 .NET 4.5

回答

0

我得到了在年底這方面的工作,通過改變我的擴展幫助以下幾點:

public static class ActionImageHelper 
{ 

    public static IHtmlString ImageActionLink(this HtmlHelper htmlHelper, string dialogTitle, string linkText, string action, 
     string controller, object routeValues, object htmlAttributes, string imageSrc) 
    { 
     var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext); 
     var img = new TagBuilder("img"); 
     img.Attributes.Add("src", VirtualPathUtility.ToAbsolute(imageSrc)); 
     TagBuilder builder = new TagBuilder("a") { InnerHtml = img.ToString(TagRenderMode.SelfClosing) }; 
     builder.Attributes["href"] = urlHelper.Action(action, controller, routeValues); 
     builder.Attributes["title"] = dialogTitle; 
     builder.MergeAttributes(new RouteValueDictionary(htmlAttributes)); 
     return MvcHtmlString.Create(builder.ToString()); 

    } 

} 

我加一個額外的參數dialogTitle其中我傳入標題,然後在擴展方法內設置標題屬性以設置標題。

,改變了JavaScript函數,然後從此屬性讀取:

<script type="text/javascript"> 

    $(document).ready(function() { 
     $("body").on("click", "a.dialog-window", null, function (e) { 

      e.preventDefault(); 
      var $link = $(this); 
      //var title = $link.text(); // Using the text prop for the title 
      var title = $(this).attr('title') //Now use the title attribute 
      $('#myModal .modal-title').html(title); 

      var url = $(this).attr('href'); 
      if (url.indexOf('#') == 0) { 
       $('#myModal').modal('show'); 
      } 
      else { 

       $.get(url, function (data) { 
        $('#myModal .contents').html(data); 
        $('#myModal').modal(); 
       }); 

      } 

     }); 
    }); 

</script> 

也許這不是最優雅的解決方案,也許有人有更好的解決辦法,但至少它意味着擴展menthod和javascript是可重用的和通用的(即沒有硬編碼標題),並且在調用ImageActionLink控件時控制標題。

這裏是我所說的ImageActionLink:

@Html.ImageActionLink("Edit Client:", "Edit", "Edit", "Client", new { id = item.i_client_id }, 
       new 
       { 

        @class = "dialog-window", 
       }, "~/Content/images/icons/open-icon-green-16.png")