
Why we use HiddenInput or ScaffoldColumn?
Suppose in our Employee Class there is a property called Employee Id and we don’t want the user to see or edit. Most model classes have atleast one such property, to acquire that functionality we’ll use HiddenInput or ScaffoldColumn Attributes in ASP.NET MVC.
HiddenInput (DisplayValue=”true”):
See Fig1. I have added HiddenInput Attribute with DisplayValue as true to Name property. When I use Html.EditorFor or Html.EditorForModel helpers then either of them will render a view for the Name property like Fig2. In Fig2 Name value is displayed as “Test”, but the user can’t edit it. The helper along with View also includes hidden input element for thatName property, So that the value of the Name property will be included in any form submissions.
If we use Html.DisplayFor instead of Html.EditorFor or Html.EditorForModel then the helper will render Only a View but not hidden input element.
HiddenInput (DisplayValue=”false”):
See Fig1 I have added HiddenInput Attribute with DisplayValue as false to Gender property. When I use Html.EditorFor or Html.EditorForModel helpers include hidden input element for that Gender property, So that the value of the Gender property will be included in any form submissions.
If we use Html.DisplayFor instead of Html.EditorFor or Html.EditorForModel then the helper will neither render norcreate hidden input element.
ScaffoldColumn (false):
If you want to exclude the property from the generated HTML, we can use theScaffoldColumn Attribute to the required property.
While creating a view for an action method if you select any scaffold Template then code will be automatically generated. While Scaffold Template generating a code for a view if it find any Scaffold Column like Fig1 then it’ll will skip generating code for that particular property. So the ScaffoldColumn Annotated property will no longer available.ScaffoldColumn won’t work for Html.DisplayFor or Html.EditorFor Helpers. This ScaffoldColumn will work only for Html.DisplayForModel Helper.
If we use Html.DisplayForModel Helper this one will automatically loop each and every property of our stronglyTypedView and it will generate HTML without ScaffoldColumn Annotated property. See Fig1 I have used ScaffoldColumn to Designation property so Scaffold Template while generating the code for view it excluded the Designation property. Once I execute the application Designation is not displayed like Fig2.
Fig 1
Fig 2
By: Jayasankar J