Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Html.HiddenFor do?

Although I have read the documentation on Html.HiddenFor, I've not grasped what is it used for...

Could somebody explain its uses and give a short example?

Where should those helpers go in the code?

like image 595
JPCF Avatar asked Oct 05 '10 18:10

JPCF


People also ask

What is HTML HiddenFor used for?

HiddenFor<TModel,TProperty>(HtmlHelper<TModel>, Expression<Func<TModel,TProperty>>, Object) Returns an HTML hidden input element for each property in the object that is represented by the specified expression, using the specified HTML attributes.

What is hidden field in MVC?

Here we will learn how to create or use hidden fields in asp.net mvc with a simple example using HTML helpers. Before going in-depth, let's understand the definition of hidden fields. The hidden fields are controls that allow us to store data or information on a page without displaying it.

How do I use TextBoxFor in HTML?

The HtmlHelper class includes two extension methods TextBox() and TextBoxFor<TModel, TProperty>() that renders the HTML textbox control <input type="text"> in the razor view. It is recommended to use the generic TextBoxFor<TModel, TProperty>() method, which is less error prons and performs fast.

How do I hide a TextBoxFor in HTML?

The Html. Hidden() method generates a input hidden field element with specified name, value and html attributes. Visit MSDN to know all the overloads of Hidden() method.


1 Answers

It creates a hidden input on the form for the field (from your model) that you pass it.

It is useful for fields in your Model/ViewModel that you need to persist on the page and have passed back when another call is made but shouldn't be seen by the user.

Consider the following ViewModel class:

public class ViewModel {     public string Value { get; set; }     public int Id { get; set; } } 

Now you want the edit page to store the ID but have it not be seen:

<% using(Html.BeginForm() { %>     <%= Html.HiddenFor(model.Id) %><br />     <%= Html.TextBoxFor(model.Value) %> <% } %> 

This results in the equivalent of the following HTML:

<form name="form1">     <input type="hidden" name="Id">2</input>     <input type="text" name="Value" value="Some Text" /> </form> 
like image 61
Justin Niessner Avatar answered Sep 25 '22 02:09

Justin Niessner