[英]Provide value to htmlAttributes where keys contain dash in their name in MVC view (e.g. “data-bind”)
Most of the Html helpers available in ASP.Net MVC have overloads with object htmlAttributes
. ASP.Net MVC中可用的大多数Html助手都有
object htmlAttributes
重载。 This is used to provide additional attribute values for the outputted tags. 这用于为输出的标签提供附加属性值。 While using the anonymous object notation for specifying htmlAttributes value, their property names must be valid c# identifier.
使用匿名对象表示法指定htmlAttributes值时,其属性名称必须是有效的c#标识符。
Now the problem arises when you are trying to output a property with a dash -
character (for eg knockout js's "data-bind" attribute) 现在的问题,当你试图输出特性用破折号出现
-
字符(如淘汰赛JS的“数据绑定”属性)
So for example lets take the following example: 例如,让我们看一下以下示例:
@Html.TextBox("Title", string.Empty, new { data-bind="text: title" })
Try the above code in your view and at run-time it would show error screen with below message: 在您的视图中尝试上面的代码,在运行时它将显示错误屏幕,并显示以下消息:
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.
So the question is, how to provide htmlAttributes with their property keys having dash characters; 所以问题是,如何为htmlAttributes提供带有短划线字符的属性键; like "data-bind"?
喜欢“数据绑定”?
In your property names, replace all your dash -
characters with an underscore _
(as shown in example below): 在你的属性名称,更换所有的破折号
-
用下划线_
:(如下面的例子所示)
@Html.TextBox("Title", string.Empty, new { data_bind="text: title" })
This would work because all HTML helpers convert an underscore _
in a property name to a dash -
when rendering the HTML; 这工作,因为所有的HTML辅助转换下划线
_
在属性名破折号-
渲染HTML时; ie for your example, data_bind
when outputted in html gets converted to data-bind
. 即,对于您的示例,在html中输出的
data_bind
将转换为data-bind
。
This is not always correct. 这并不总是正确的。 Say you're using parameters in a URL.
假设您正在使用URL中的参数。
@Html.ActionLink("Add Job", "Index", "Home", new { foo_bar = "foobar" }, new { @class = "btn btn-default", data_foo = "bar" })
The data_foo does get rendered as "data-foo", but the parameters stays as a under bar. data_foo确实呈现为“data-foo”,但参数保持为下栏。 Your result will be:
http://your.domain/yourapp/?foo_bar=foobar
您的结果将是:
http://your.domain/yourapp/?foo_bar=foobar
Of course you can't actually use the dash or you get the error specified in the OP. 当然,你实际上不能使用破折号,或者你得到OP中指定的错误。
I have worked around this as follows, but I'd be interested to see if anyone that comes along in the future will have a better way: 我已经解决了以下这个问题,但是我有兴趣看看将来有没有人会有更好的方法:
@{
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('_', '-'))
Use the HtmlHelper.AnonymousObjectToHtmlAttributes
method. 使用
HtmlHelper.AnonymousObjectToHtmlAttributes
方法。 The following code will add a tooltip to the text input box, with the tooltip displaying the data from the DisplayAttribute description for MyIntVal on my model. 以下代码将向文本输入框添加工具提示,工具提示在我的模型上显示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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.