razor - 我如何知道 Razor Html 帮助程序生成什么 id?

标签 razor html-helper id-generation

在 Razor View 中,假设我有这样的东西:

@Html.HiddenFor(model => model.Värde)

这里,“Värde”是瑞典语“值(value)”的意思。注意它包含“ä”。为此,Razor 将生成如下 HTML:

<input id="V_rde" name="Värde" type="hidden" />

请注意,元素的 id 是“V_rde”,而不是“Värde”。

那么,如何使用 getElementById 从 JavaScript 访问此元素? 我知道我可以使用“V_rde”,但我想要一些通用的东西,而不仅仅是“Värde”,像这样(意思是 htmlAttributes 参数):

@Html.Whatever(..., new { onclick = "JavaScript:alert(document.getElementById('@Html.IdFor("Värde")'));" })

我希望 @Html.IdFor(string name) 转换名称,就像在 @Html 帮助器中转换标签、复选框、编辑框等的 id 一样。

最佳答案

同时 @Html.IdFor(x=>x.YourProperty) 将其纳入 MVC,我认为这是现在最简单的解决方案

关于razor - 我如何知道 Razor Html 帮助程序生成什么 id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11021604/

相关文章:

asp.net-mvc-4 - Razor MVC 内圆角

jquery - 单击编辑特定行时,标签将变为文本框

asp.net - 在 Web.Config 中添加 HtmlHelper NameSpace 不起作用

hibernate - org.hibernate.MappingException : The increment size of the sequence is set to [10] in the entity mapping while . ..大小为[1]

asp.net-mvc - Razor View 中的 'pattern' 属性警告

javascript - 传递 JSON 对象时 View 中出现 Visual Studio "Conditiional compilation is turned off"错误

HTML.Checkboxfor 到切换按钮 HTML?

asp.net-mvc-3 - 在 ASP.NET MVC 3 View 中呈现 System.Drawing.Image 的最佳方式

hibernate 。增强的标识符生成器

java - hibernate 中的 native 生成器类