How to use Html.HiddenFor in MVC

Home > Search > How-to
  by

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" />

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments