2017-06-02 11 views
0

文字列を変換する方法です(この文字列はasp-route -...属性を含む)をhtml属性のリストに変更できますか?かみそりエンジンは、すべてのasp-route -...属性を使用して正しいURLに変換する必要があります。私は次のコードを持っていますが、うまくいきません。文字列をHTML属性に変換する

@{ 
    var Attributes  = ViewData["Attributes"] as Dictionary<string,string>; 
    var AttributeRoute = ""; 

    @foreach (var key in Attributes.Keys) 
    { 
     AttributeRoute += "asp-route-"+key+"=\""+Attributes[key]+"\" "; 
    } 
} 

... 

@AttributeRoute #Prints output (ex. asp-route-testkey="testvalue") 

<a class='item' @AttributeRoute>test</a> #Doesn't print the list of attributes 

答えて

0

は、次の手順を実行して、それを自分で解決:

1.リンク(.cshtmlファイル内)

<special-link>link</special-link> 

を作成

namespace test 
{ 
    [HtmlTargetElement("special-link")] 
    public class SpecialLinkTagHelper : TagHelper 
    { 
     [ViewContext] 
     public ViewContext ViewContext { get; set; } 

     public override void Process(TagHelperContext context, TagHelperOutput output) 
     { 
      //Create a tag 
      output.TagName="a"; 

      //Get the parameters 
      string parameters=""; 
      Dictionary<string,string> Parameters = (Dictionary<string,string>)this.ViewContext.ViewData["Attributes"]; 
      foreach(KeyValuePair<string, string> pair in Parameters){ 
       parameters+= pair.Key+"="+pair.Value+"&"; 
      } 

      output.Attributes.SetAttribute("href", "?"+parameters); 
     } 
    } 
} 

2.カスタムTaghelperクラスを作成します。これが誰かを助けることができるといいですね!

関連する問題