Asp.Net Mvc Hidden Field from Data Annotations

asp.netasp.net MvcData Annotations

asp.net Problem Overview


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.

asp.net Solutions


Solution 1 - asp.net

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJustin SolizView Question on Stackoverflow
Solution 1 - asp.netmiensolView Answer on Stackoverflow