C# 为htmlAttributes提供值,其中键在MVC视图中的名称中包含破折号(例如“数据绑定”)
ASP.NETMVC中可用的大多数Html帮助程序都有C# 为htmlAttributes提供值,其中键在MVC视图中的名称中包含破折号(例如“数据绑定”),c#,asp.net-mvc,asp.net-mvc-4,knockout.js,html,C#,Asp.net Mvc,Asp.net Mvc 4,Knockout.js,Html,ASP.NETMVC中可用的大多数Html帮助程序都有对象htmlAttributes的重载。这用于为输出的标记提供附加属性值。使用匿名对象表示法指定htmlAttributes值时,其属性名必须是有效的c#标识符 现在,当您试图输出带有破折号-字符的属性(例如,knockout js的“data bind”属性)时,问题就出现了 例如,让我们以以下示例为例: @Html.TextBox("Title", string.Empty, new { data-bind="text: title" }
对象htmlAttributes
的重载。这用于为输出的标记提供附加属性值。使用匿名对象表示法指定htmlAttributes值时,其属性名必须是有效的c#标识符
现在,当您试图输出带有破折号-
字符的属性(例如,knockout js的“data bind”属性)时,问题就出现了
例如,让我们以以下示例为例:
@Html.TextBox("Title", string.Empty, new { data-bind="text: title" })
在您的视图中尝试上述代码,运行时将显示错误屏幕,并显示以下消息:
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0746: Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.
所以问题是,如何为htmlAttributes提供具有破折号字符的属性键;像属性名称中的“数据绑定”一样,将所有破折号
-
字符替换为下划线(如下例所示):
这将起作用,因为在呈现HTML时,所有HTML帮助程序都会将属性名称中的下划线\uu
转换为破折号-
;i、 例如,数据绑定
以html格式输出时,在属性名称中转换为数据绑定
,用下划线替换所有破折号-
字符(如下例所示):
这将起作用,因为在呈现HTML时,所有HTML帮助程序都会将属性名称中的下划线\uu
转换为破折号-
;i、 例如,当以html格式输出时,数据绑定
被转换为数据绑定
这并不总是正确的。假设您正在URL中使用参数
@Html.ActionLink("Add Job", "Index", "Home", new { foo_bar = "foobar" }, new { @class = "btn btn-default", data_foo = "bar" })
数据_foo确实呈现为“数据foo”,但参数保持为下栏。您的结果将是:http://your.domain/yourapp/?foo_bar=foobar
当然,你不能实际使用破折号,否则你会得到OP中指定的错误
我在这方面做了如下工作,但我有兴趣看看未来是否有人会有更好的方法:
@{
var link = Html.ActionLink("Add Job", "Index", "Home", new { foo_bar = "foobar" }, new { @class = "btn btn-default", data_foo = "bar" });
}
@Html.Raw(link.ToString().Replace('_', '-'))
这并不总是正确的。假设您正在URL中使用参数
@Html.ActionLink("Add Job", "Index", "Home", new { foo_bar = "foobar" }, new { @class = "btn btn-default", data_foo = "bar" })
数据_foo确实呈现为“数据foo”,但参数保持为下栏。您的结果将是:http://your.domain/yourapp/?foo_bar=foobar
当然,你不能实际使用破折号,否则你会得到OP中指定的错误
我在这方面做了如下工作,但我有兴趣看看未来是否有人会有更好的方法:
@{
var link = Html.ActionLink("Add Job", "Index", "Home", new { foo_bar = "foobar" }, new { @class = "btn btn-default", data_foo = "bar" });
}
@Html.Raw(link.ToString().Replace('_', '-'))
使用HtmlHelper.AnonymousObjectToHtmlAttributes
方法。以下代码将向文本输入框添加工具提示,其中工具提示显示模型上MyIntVal的DisplayAttribute描述中的数据
@{
var htmlAttributesWithDashes = HtmlHelper.AnonymousObjectToHtmlAttributes(
new
{
id = "myTextBoxId",
data_toggle = "tooltip",
data_position = "left top",
title = ModelMetadata.FromLambdaExpression( m => m.MyIntVal, ViewData ).Description
}
);
}
<div class="col-sm-6">
@Html.TextBoxFor( m => m.MyIntVal, htmlAttributesWithDashes )
</div>
@{
var htmlAttributesWithDashes=HtmlHelper.AnonymousObjectToHtmlAttributes(
刚出现的
{
id=“myTextBoxId”,
数据\u toggle=“工具提示”,
数据位置=“左上”,
title=modelmetada.FromLambdaExpression(m=>m.MyIntVal,ViewData)。说明
}
);
}
@TextBoxFor(m=>m.MyIntVal,htmlAttributesWithDash)
使用HtmlHelper.AnonymousObjectToHtmlatAttributes
方法。以下代码将向文本输入框添加工具提示,其中工具提示显示模型上MyIntVal的DisplayAttribute描述中的数据
@{
var htmlAttributesWithDashes = HtmlHelper.AnonymousObjectToHtmlAttributes(
new
{
id = "myTextBoxId",
data_toggle = "tooltip",
data_position = "left top",
title = ModelMetadata.FromLambdaExpression( m => m.MyIntVal, ViewData ).Description
}
);
}
<div class="col-sm-6">
@Html.TextBoxFor( m => m.MyIntVal, htmlAttributesWithDashes )
</div>
@{
var htmlAttributesWithDashes=HtmlHelper.AnonymousObjectToHtmlAttributes(
刚出现的
{
id=“myTextBoxId”,
数据\u toggle=“工具提示”,
数据位置=“左上”,
title=modelmetada.FromLambdaExpression(m=>m.MyIntVal,ViewData)。说明
}
);
}
@TextBoxFor(m=>m.MyIntVal,htmlAttributesWithDash)