Asp.Net Mvc Hidden Field from Data Annotations

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 21:57:34

问题


I thought this would be a quick search on google but maybe I'm missing something. Is there a way, using Data Annotations, to set a ViewModel property to create a HiddenInput when the markup get rendered?

The only annotations I've found were to hide the property from the view entirely, I still want the property rendered but as a hidden input.


回答1:


This property:

[System.Web.Mvc.HiddenInput(DisplayValue = false)]
public int Id { get; set; }

will be rendered as i.e.

<input id="Id" name="Id" type="hidden" value="21" />

when using Html.EditorForModel() or Html.EditorFor(m => m.Id)



来源:https://stackoverflow.com/questions/4542972/asp-net-mvc-hidden-field-from-data-annotations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!