我已经在我的asp.net mvc 5编辑器模板中使用了Chris Pratt的MergeHtmlAttributes Html Helper Extension方法一段时间了。我开始将应用程序切换到Asp.net核心1.1(.net framework 4.5.2)。并且htmlhelperExtension对我不起作用。
public static partial class HtmlHelperExtensions
{
//https://cpratt.co/html-editorfor-and-htmlattributes/
public static IDictionary MergeHtmlAttributes(this HtmlHelper helper, object htmlAttributesObject, object defaultHtmlAttributesObject)
{
var concatKeys = new string[] { "class" };
var htmlAttributesDict = htmlAttributesObject as IDictionary;
var defaultHtmlAttributesDict = defaultHtmlAttributesObject as IDictionary;
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;
}
}
当我复制该类时,标记该语句:using System.Web.Mvc; - 不解析符号MVC。
删除使用语句后,我得到的消息无法解析符号" HtmlHelper"在MergeHtmlAttributes(this HtmlHelper helper, ...)
我可以选择添加其中一个
Microsoft.AspNetCore.Mvc.ViewFeatures.HtmlHelper或.HtmlHelper我选择了.HtmlHelper。
之后它引用了RouteValueDictionary行htmlAttributes =并表示它无法将IDictionary转换为system.web.Routing.RouteValueDictionary。我应该将类型更改为IDictionary或转换为RouteValueDictionary。无论哪种方式,当我尝试在我的一个编辑器模板中使用MergeHtmlAttributes时,我都会收到以下错误。
'IHtmlHelper'不包含' MergeHtmlAttributes'的定义。和最好的扩展方法重载' HtmlHelperExtensions.MergeHtmlAttributes(HtmlHelper,object,object)'需要一个类型为' HtmlHelper'的接收器
此行正在抛出错误 - > var htmlAttributes = Html.MergeHtmlAttributes(ViewData, defaultHtmlAttributesObject);
有没有办法让它在asp.net核心中运行,还是有不同的方法来实现相同的结果?这是我的一个编辑器模板的示例,因此您可以看到正在使用的MergeHtmlAttributes。如果我不能再像这样构建模板,那么有更新/更好的方法来使用标记助手吗?我真的很喜欢在一个html助手中使用labelfor,txtboxfor,ValidationMessageFor等。
@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 validationAttributes = Html.GetUnobtrusiveValidationAttributes("");
Html.ViewContext.FormContext.RenderedField(ViewData.TemplateInfo.GetFullHtmlFieldName(null), false);
}
@Html.LabelFor(x => x, new { @class = "control-label" })
@if (validationAttributes.ContainsKey("data-val-required"))
{*}
@Html.TextBoxFor(x => x, htmlAttributes)
@Html.ValidationMessageFor(model => model, "", new { @class = "text-danger" })
F.Y.I。转换到asp.net核心1.1(和.net框架4.5.2)时,我最终将连接字符串放在它的app.config文件中,该文件允许EF6与Asp.net核心一起工作,这样我就可以继续使用我构建的EF代码,无论出于何种原因,它都无法在appsettings.json中找到连接字符串。