简体   繁体   English

为htmlAttributes提供值,其中键在MVC视图中的名称中包含破折号(例如“data-bind”)

[英]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确实呈现为“da​​ta-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.

相关问题 提供值给htmlAttributes,其中键是MVC视图中c#保留的关键字名(例如,css“ class”) - Provide value to htmlAttributes where keys are c# reserved keyword name in MVC view (e.g. css “class”) 无法反序列化当前的 JSON object(例如 {“name”:“value”} - Cannot deserialize the current JSON object (e.g. {“name”:“value”} 无法反序列化当前JSON对象(例如{“ name”:“ value”}) - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) 无法反序列化当前的 JSON 对象(例如 {“name”:“value”}) - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) 无法将JSON数组(例如[1,2,3])反序列化为类型&#39;&#39;,因为类型需要JSON对象(例如{“ name”:“ value”}) - Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {“name”:“value”}) 无法将当前 JSON object(例如 {“name”:“value”})反序列化为类型需要 JSON 数组(例如 [1,2,3] 才能正确反序列化) - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type requires a JSON array (e.g. [1,2,3]) to deserialize correctly 无法将当前JSON数组(例如[1,2,3])反序列化为类型”,因为该类型需要JSON对象(例如{“ name”:“ value”}) - Cannot deserialize the current JSON array (e.g. [1,2,3]) into type '' because the type requires a JSON object (e.g. {“name”:“value”}) cshtml-通过数据绑定值调节类 - cshtml - conditioning class by data-bind value 无法将当前 JSON 对象(例如 {&quot;name&quot;:&quot;value&quot;})反序列化为类型 &#39;Value[]&#39;,因为该类型需要 JSON 数组(例如 [1,2,3]) - Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'Value[]' because the type requires a JSON array (e.g. [1,2,3]) 如果没有填充到预期类型的​​声明变量,那么来自例如方法调用的返回值会在哪里? - Where does the returned value from e.g. a method call go if not filled into a declared variable of expected type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM