如何指定为Html.TextBox辅助属性,同时保持从ViewData的价值检索? [英] How do I specify attributes for a Html.TextBox helper while maintaining the value retrieval from ViewData?
问题描述
我使用Html.TextBox帮助创建文本框。我想设置文本框,我的理解使用以下过载完成属性:
I am using the Html.TextBox helper to create textboxes. I want to set attributes on the textbox, which I understand is done using the following overload:
Html.TextBox(字符串名称,对象的值,对象htmlAttributes)
不过,我想保持那里的HTML辅助自动无论从ViewData的或ViewData.Model使用值的功能,我不明白的方式,只是指定名称和htmlAttributes。这可能吗?
However, I want to maintain the functionality where the HTML helper automatically uses the value from either ViewData or ViewData.Model and I do not see a way to just specify the name and the htmlAttributes. Is this possible?
推荐答案
看源$ C $之后ç,似乎所有你需要做的就是指定在需要一个名称,值和htmlAttributes签名为空值。如果该值为null,它会尝试使用从ViewData的价值。
After looking at the source code, it appears that all you need to do is specify the value as null in the signature that takes a name, value, and htmlAttributes. If the value is null, it will attempt to use the value from the ViewData.
Html.TextBox( "name", null, new { @class = "css-class" } );
这篇关于如何指定为Html.TextBox辅助属性,同时保持从ViewData的价值检索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!