Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net Mvc Hidden Field from Data Annotations

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.

like image 776
Justin Soliz Avatar asked Dec 28 '10 00:12

Justin Soliz


People also ask

Which data annotation attribute allows the hiding of fields from editor forms?

ScaffoldColumn – Allows hiding fields from editor forms.

How do you set the value of a hidden field from a controller in MVC?

You can transfer value from controller using ViewData[""] . ViewData["hdnFlag"] = userId; return View(); Now, In you view.

How can access hidden field in asp net code behind?

Right click on project and select Add-->Add New Item and select Web Form. Add a new Web Form called "Default. aspx". Now, drag and drop the HiddenField Control on the page.


1 Answers

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)

like image 192
miensol Avatar answered Oct 16 '22 06:10

miensol