ASP.Net MVC回发一个标签值到控制器 [英] ASP.Net MVC Postback a label value to your controller
问题描述
当使用EditorTemplate,如果我想显示在屏幕上的模型上的价值,也被调回控制器,我应该用什么帮助?
IE浏览器。如果我使用TextBoxFor:
@ Html.TextBoxFor(型号=> model.RoomTypeName)
...那么用户可以修改文本...
我宁愿只显示文本,但如果我使用:
@ Html.DisplayTextFor(型号=> model.RoomTypeName)
......那么这是不是回发到控制器。
所以我是可以显示文本,并确保我的模型状态是有效的,添加第二个隐藏字段,只有这样,例如:
@ Html.DisplayTextFor(型号=> model.RoomTypeName)
@ Html.HiddenFor(型号=> model.RoomTypeName)
我知道的作品,但我不知道是否有这样做的更优雅的方式 - 这样我就可以显示该值,并张贴回来,而不需要复制它作为一个隐藏的元素也
感谢您,
标记
@ Html.DisplayTextFor(型号=> model.RoomTypeName)
@ Html.HiddenFor(型号=> model.RoomTypeName)
这是非常干净,做你想达到什么样的标准方式。
如果你想使自己的HTML辅助但这正是通过保存一行只会混淆谁可能会读取您在未来的code,甚至自己其他人同样的事情吧。
When using an EditorTemplate, if I want a value on the model to be displayed on the screen, but also to be posted back to the controller, what helper should I use?
ie. if I use TextBoxFor:
@Html.TextBoxFor(model => model.RoomTypeName)
...then the user can amend the text...
I would rather just show the text, but if I use:
@Html.DisplayTextFor(model => model.RoomTypeName)
...then that is not posted back to the controller.
So is the only way I can display the text, and also to ensure my model state is valid, to add a second hidden field, eg:
@Html.DisplayTextFor(model => model.RoomTypeName)
@Html.HiddenFor(model => model.RoomTypeName)
I know that works, but I'm wondering if there is a more elegant way of doing it - so I can display the value, and post it back, without the need to replicate it as a hidden element also?
Thank you,
Mark
@Html.DisplayTextFor(model => model.RoomTypeName)
@Html.HiddenFor(model => model.RoomTypeName)
This is very clean and standard way of doing what you want to achieve.
If you would make your own HTML helper that does exactly the same thing it by saving one line would just confuse other people who might read your code in the future, or even yourself.
这篇关于ASP.Net MVC回发一个标签值到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!