MVC - html.HiddenFor Helper

by
Jeremy Canfield |
Updated: March 20 2020
| MVC articles
Html.HiddenFor is often used with forms, such as when adding a new record into SQL or updating a record in SQL. For example, let's say a record was added to SQL where published_by equals John Doe. When using a form to update the record, we don't want to have to add John Doe again, and we want the database to retain John Doe. The following HTML Helper would be used.
@Html.HiddenFor(model => model.published_by)
This would create the following HTML.
<input id="published_by" name="published_by" type="hidden" value="John Doe" />
Date Updated
Let's say SQL has a date_updatedcolumn. We could use the following to update the date_updated column to the current date.
@Html.HiddenFor(model => model.date_updated, new { @Value = System.DateTime.Now })
This would create the following HTML.
<input Value="12/06/2016 19:22:57" id="date_updated" name="date_updated" type="hidden" value="12/6/2016 12:00:00 AM" />
Did you find this article helpful?
If so, consider buying me a coffee over at