2017-05-13 5 views
1

私はasp.net mvc 5エディタテンプレートでしばらくChris PrattのMergeHtmlAttributes HTMLヘルパー拡張メソッドを使用してきました。私はAsp.netコア1.1(.netフレームワーク4.5.2)にアプリケーションを切り替えるプロセスを開始しました。そして、htmlhelperExtensionは私のために働いていません。asp.netコアのHtmlAttributesを

public static partial class HtmlHelperExtensions 
{ 
    //https://cpratt.co/html-editorfor-and-htmlattributes/ 
    public static IDictionary<string, object> MergeHtmlAttributes(this HtmlHelper helper, object htmlAttributesObject, object defaultHtmlAttributesObject) 
    { 
     var concatKeys = new string[] { "class" }; 

     var htmlAttributesDict = htmlAttributesObject as IDictionary<string, object>; 
     var defaultHtmlAttributesDict = defaultHtmlAttributesObject as IDictionary<string, object>; 

     RouteValueDictionary htmlAttributes = (htmlAttributesDict != null) 
      ? new RouteValueDictionary(htmlAttributesDict) 
      : HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributesObject); 

     RouteValueDictionary defaultHtmlAttributes = (defaultHtmlAttributesDict != null) 
      ? new RouteValueDictionary(defaultHtmlAttributesDict) 
      : HtmlHelper.AnonymousObjectToHtmlAttributes(defaultHtmlAttributesObject); 

     foreach (var item in htmlAttributes) 
     { 
      if (concatKeys.Contains(item.Key)) 
      { 
       defaultHtmlAttributes[item.Key] = (defaultHtmlAttributes[item.Key] != null) 
        ? string.Format("{0} {1}", defaultHtmlAttributes[item.Key], item.Value) 
        : item.Value; 
      } 
      else 
      { 
       if(item.Key?.ToString() == "divClass") 
       { 
        continue; 
       } 
       defaultHtmlAttributes[item.Key] = item.Value; 
      } 
     } 

     return defaultHtmlAttributes; 
    } 
} 

私がクラスをコピーすると、statmentにフラグが立てられます。using System.Web.Mvc;シンボルMVCを解決できません。 そして、そのステートメントを使用して削除した後、メッセージが "HtmlHelper"というシンボルを解決できない場合は、 Microsoft.AspNetCore.Mvc.ViewFeatures.HtmlHelperまたは.HtmlHelper<Tmodel>のいずれかを追加するか、.HtmlHelperを選択しました。 その後、RouteValueDictionaryのhtmlAttributes =行を参照し、IDictionary<string, object>system.web.Routing.RouteValueDictionaryに変換できないことを示します。タイプをIDictionary<string, object>に変更するか、RouteValueDictionaryにキャストする必要があります。どちらの方法でも、エディタテンプレートの1つでMergeHtmlAttributesを使用しようとすると、次のエラーが発生します。

'IHtmlHelper<object>'

「をMergeHtmlAttributes」と最良拡張メソッドのオーバーロード「HtmlHelperExtensions.MergeHtmlAttributes(HtmlHelperの、オブジェクト、オブジェクト)」タイプの受信機を必要とする「のHtmlHelper」の定義が含まれていないこの行はエラーを投げています - >var htmlAttributes = Html.MergeHtmlAttributes(ViewData, defaultHtmlAttributesObject);

asp.netコアでこれを動作させる方法はありますか、同じ結果を得るための別の方法がありますか?次に、使用しているMergeHtmlAttributesが表示されるように、エディタテンプレートの例を示します。このようなテンプレートを作成できない場合は、タグヘルパーを使用して新しい/優れた方法がありますか?私はlabelfor、txtboxfor、ValidationMessageForなどをすべて1つのhtmlヘルパーに持つのが本当に好きです。

@model int? 

@{ 
    var defaultHtmlAttributesObject = new { @class = "form-control" }; 
    var htmlAttributes = Html.MergeHtmlAttributes(ViewData, defaultHtmlAttributesObject); 

    object divClass; 
    ViewData.TryGetValue("divClass", out divClass); 
    if (divClass == null) { divClass = ""; } 

    IDictionary<string, object> validationAttributes = Html.GetUnobtrusiveValidationAttributes(""); 
    Html.ViewContext.FormContext.RenderedField(ViewData.TemplateInfo.GetFullHtmlFieldName(null), false); 
} 

<div class="form-group @divClass @(Html.ValidationErrorFor(x => x, " has-error"))"> 
    @Html.LabelFor(x => x, new { @class = "control-label" }) 
    @if (validationAttributes.ContainsKey("data-val-required")) 
    {<span class="text-danger">*</span>} 
    @Html.TextBoxFor(x => x, htmlAttributes) 
    @Html.ValidationMessageFor(model => model, "", new { @class = "text-danger" }) 
</div> 

F.Y.I. asp.netコア1.1(および.netフレームワーク4.5.2)に変換中に、私は接続文字列をapp.configファイルに入れて、EF6をAsp.netコアで動作させることができたので、私が持っていたEFコードどのような理由であれ、appsettings.jsonに接続文字列が見つかりませんでした。

+0

私は関数定義はされるべきだと思う: パブリック静的のIDictionary <文字列、オブジェクト> MergeHtmlAttributes(このIHtmlHelperヘルパー、htmlAttributesObjectオブジェクト、defaultHtmlAttributesObjectオブジェクト) 私はその後、私は、「シンボルAnonymousObjectToHtmlAttributesを解決できません」を取得することをやるたらIHtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributesObject); AnonymousObjectToHtmlAttributesがここでgithubに実装されていた方法を発見しました: https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewFeatures/HtmlHelper.cs –

答えて

0

最初は、マイクロソフトがHtmlHelperではなくIHtmlHelperを使用していたことに気付きませんでした。かつて、その変更を反映するようにコードを修正し、Microsoftのmvcリポジトリがgithubで見つかったので、AnonymousObjectToHtmlAttributesの実装を見つけることができました。以下のコードは私のために働いていますが、私はまだ考えていないエッジケースに対していくつかの変更を加えなければならないかもしれません。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web.Routing; 

using Microsoft.AspNetCore.Mvc.Rendering; 

public static class HtmlHelperExtensions 
{ 
    //http://cpratt.co/html-editorfor-and-htmlattributes/ 
    /// <summary> 
    /// This is used with the EditorTemplates to copy the page element's htmlAttributes over to the editorTemplates 
    /// while still being able to specify values to always use (like @class = "form-control") in the editorTemplates. 
    /// </summary> 
    public static IDictionary<string, object> MergeHtmlAttributes(this IHtmlHelper helper, object htmlAttributesObject, object defaultHtmlAttributesObject) 
    { 
     var concatKeys = new[] { "class" }; 

     var htmlAttributesDict = htmlAttributesObject as IDictionary<string, object>; 
     var defaultHtmlAttributesDict = defaultHtmlAttributesObject as IDictionary<string, object>; 

     RouteValueDictionary htmlAttributes = new RouteValueDictionary(htmlAttributesDict != null 
      ? htmlAttributesDict 
      : AnonymousObjectToHtmlAttributes(htmlAttributesObject)); 

     RouteValueDictionary defaultHtmlAttributes = new RouteValueDictionary(defaultHtmlAttributesDict != null 
      ? defaultHtmlAttributesDict 
      : AnonymousObjectToHtmlAttributes(defaultHtmlAttributesObject)); 

     foreach (var item in htmlAttributes) 
     { 
      if (concatKeys.Contains(item.Key)) 
      { 
       defaultHtmlAttributes[item.Key] = defaultHtmlAttributes[item.Key] != null 
        ? string.Format("{0} {1}", defaultHtmlAttributes[item.Key], item.Value) 
        : item.Value; 
      } 
      else 
      { 
       if(item.Key == "divClass") 
       { 
        continue; 
       } 
       defaultHtmlAttributes[item.Key] = item.Value; 
      } 
     } 

     return defaultHtmlAttributes; 
    } 

    private static IDictionary<string, object> AnonymousObjectToHtmlAttributes(object htmlAttributes) 
    { 
     var dictionary = htmlAttributes as IDictionary<string, object>; 
     if (dictionary != null) 
     { 
      return new Dictionary<string, object>(dictionary, StringComparer.OrdinalIgnoreCase); 
     } 

     dictionary = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase); 

     if (htmlAttributes != null) 
     { 
      var stringAttributes = htmlAttributes.ToString(); 
      stringAttributes = stringAttributes.Replace("{", "").Replace("}", ""); 
      string[] attributesArray = stringAttributes.Split(new[] { ','}, StringSplitOptions.RemoveEmptyEntries); 

      foreach (var helper in attributesArray) 
      { 
       string[] attribKeyValue = helper.Trim().Split(' '); 
       dictionary[attribKeyValue.First()] = attribKeyValue.Last(); 
      } 
     } 

     return dictionary; 
    } 
}