问题描述
我认为Html.HiddenFor
可以使用Html.DisplayFor
或Html.EditorFor
之类的模板.不幸的是,该方法不像其他方法那样接受TemplateName
.
I thought Html.HiddenFor
could use Templates like Html.DisplayFor
or Html.EditorFor
. Unfortunately the method doesn't accept a TemplateName
like the others.
我知道,解决方法是使用具有HiddenFors的DisplayFor/EditorFor模板.但我想找出如何扩展Html.HiddenFor
方法.有人吗?
I know, the workaround would be to use a DisplayFor/EditorFor Template which has HiddenFors. But I would like to find out how to extend the Html.HiddenFor
method. Anyone?
致谢
推荐答案
似乎您被错误的类比误导了. HiddenFor
与<input type="hidden"/>
标记完全对应.就像TextBoxFor
,CheckBoxFor
等一样.这些方法并非旨在使用模板.另一端的DisplayFor
/EditorFor
是专门创建的,可与项目中定义的模板一起使用.因此,您所要求的是无法即用的.
Seems like you are mislead by wrong analogy. HiddenFor
corresponds exactly to the <input type="hidden"/>
tag. Just like TextBoxFor
, CheckBoxFor
etc. These methods are not designed to use templates. DisplayFor
/EditorFor
on the other side are specially created to be used with templates defined in the project. Thus what you are asking for is not possible out-of-the-box.
但是,您始终可以使用任何参数集和所需的逻辑为HiddenFor
定义自己的重载.
However you can always define your own overload for HiddenFor
with whatever set of parameters and whatever logic you might require.
这篇关于在ASP.NET MVC中扩展HiddenFor模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!