是否Html.EditorFor支持比类更MVC5.1? [英] Does Html.EditorFor support more than class in MVC5.1?

查看:148
本文介绍了是否Html.EditorFor支持比类更MVC5.1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在不修改我的编辑模板,是有可能通过其他HTML属性到 Html.EditorFor 助手扩展?

Without modifying my Editor Templates, is it possible to pass other HTML attributes into the Html.EditorFor Helper extension?

此示例显示了类正在传递:

The example shows a class being passed in:

@Html.EditorFor(model => model.information, new { htmlAttributes = new { @class = "form-control" }})

有许多潜在的情景,但我想我的装点输入端与数据MYVAR =值。当我尝试这个,我得到编译器错误无效的匿名类型宣告

There are many potential scenarios but I want to decorate my input with a data-myvar="value". When I try this I get compiler errors invalid anonymous type declaratory.

@Html.EditorFor(model => model.information, new { htmlAttributes = new { @data-myvar="value" }})

此外,如果有可能可以我在除了一类通过这个?即通过在 htmlattributes 的数组。我挣扎着从发行说明中除了找到任何文件。

Additionally if it is possible can I pass this in in addition to a class? i.e. pass in an array of htmlattributes. I'm struggling to find any documentation apart from the release notes.

<一个href=\"http://weblogs.asp.net/jgalloway/archive/2014/01/28/looking-at-asp-net-mvc-5-1-and-web-api-2-1-part-3-bootstrap-and-javascript-enhancements.aspx\"相对=nofollow>约翰Galaway的文章

推荐答案

它做,而是因为.NET如何处理匿名类型(感谢马克),您需要将破折号更改为下划线。此外,当您声明HTML中的类属性的字典(因为类是保留字),只需要在 @ 符号。声明数据时,您可以将其关闭。 - 元素

It does, but because of how .NET handles anonymous types (thanks Mark), you need to change the dash to an underscore. Also the @ symbol is only needed when you are declaring the class in the Html attributes dictionary (since class is a reserved word). You can leave that off when declaring data- elements.

@Html.EditorFor(model => model.information, 
      new { htmlAttributes = new { data_myvar="value" }})

当这是由助手解析,破折号实际上将在HTML呈现出来。

When this is parsed by the helper, a dash will actually be rendered out in the HTML.

<input type="text" id="information" name="information" data-myvar="value"/>

要传递多个属性,只是一个逗号分隔值

To pass in multiple attributes, just separate the values by a comma

@Html.EditorFor(model => model.information, 
      new { htmlAttributes = new { data_myvar="value", data_othervar = "something" }})

这篇关于是否Html.EditorFor支持比类更MVC5.1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆