.net Html.EditorFor additionalViewData

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4457136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:02:46  来源:igfitidea点击:

Html.EditorFor additionalViewData

.netasp.net-mvc-3

提问by Boo

I have a custom editor template where I add values to the ViewDatalike so:

我有一个自定义编辑器模板,我可以在其中添加值,如下ViewData所示:

@Html.EditorFor( model => model.PhoneNumber , new { Title = "SomeValue" } )

How can I access both the value and the property name?

如何访问值和属性名称?

回答by SLaks

ViewData is a dictionary.

ViewData 是一个字典。

You can write ViewData["Title"], or you can loop through ViewData(which is a collection of KeyValuePairs) or ViewData.Keys.

您可以编写ViewData["Title"],也可以循环遍历ViewData(它是 KeyValuePairs 的集合)或ViewData.Keys.

回答by FRj

You can nest your htmlAttributes object in view data:

您可以在视图数据中嵌套 htmlAttributes 对象:

<%= Html.EditorFor(model => model.PhoneNumber, new { htmlAttributes = new { Title = "SomeValue" } })

Then in your editor template:

然后在您的编辑器模板中:

<%= Html.TextBox("", Model.Value, ViewData["htmlAttributes"])%>