It provides an easy way to Render HTML in our View.
The following is the list of Html Helper controls.
- Html.Beginform
- Html.EndForm
- Html.Label
- Html.TextBox
- Html.TextArea
- Html.Password
- Html.DropDownList
- Html.CheckBox
- Html.RedioButton
- Html.ListBox
- Html.Hidden
Below are Strongly Type Html Helper methods, this will allow us to check compile time errors. We get Model's Property intelligence at Runtime.
- Html.LabelFor
- Html.TextBoxFor
- Html.TextAreaFor
- Html.DropDownListFor
- Html.CheckBoxFor
- Html.RadioButtonFor
- Html.ListBoxFor
- Html.HiddenFor
Here we have Label, Textbox, Password, Rediobutton, DropDown and Checkbox.
In ASP.NET Application we have Server Controls, so we just need to drag and drop from the Toolbox and design the page easily, but here in MVC Application we don't have Server Control so either we can use html or Html Helper Class Controls.
One more important thing here is in MVC there is no ViewState, so managing controls value here is challenging.
Create a new MVC Project, go to Files - > New Project -> ASP.NET Web Application and Select MVC Template. Give a name and Click on Ok.
Right click on Model folder and add new Class 'Register.cs'.
Create a property for the Register class as follows.
- public class Register
- {
- public string Name { get; set; }
- public string Phone { get; set; }
- public string Email { get; set; }
- public string Gender { get; set; }
- public string Password { get; set; }
- public string Country { get; set; }
- public bool Terms { get; set; }
- }
Let's use HomeController and it's view 'Index.cshtml'
In the html part let's start the with Html.BeginForm.
That is the main container, we will place all controls inside the tag.
That is the main container, we will place all controls inside the tag.
- @using (Html.BeginForm("Index", "Home", FormMethod.Post))
- {
- }
If you are not passing any parameter like View name and Controller name then it will automatically call the current controller action method having [HttpPost] attribute.
Inside the Form Let's Start with Html.Lable : @Html.Label("UserName").
This will create a Lable Named UserName, so for Creating Label we will use @Html.Lable
This will create a Lable Named UserName, so for Creating Label we will use @Html.Lable
Next, We need Textbox for the UserName, for that we use Html.TextBox same as @Html.TextBox("UserName").
This will Create a TextBox Named UserName.
- @using (Html.BeginForm("Index", "Home", FormMethod.Post))
- {
- <br />
- @Html.Label("UserName")
- @Html.TextBox("UserName")
- <br />
- }
Same way we will design UserPhone, UserEmail etc.
- @using (Html.BeginForm("Index", "Home", FormMethod.Post))
- {
- <br />
- @Html.Label("UserName")
- @Html.TextBox("UserName")
- <br />
- @Html.Label("UserPhone")
- @Html.TextBox("UserPhone")
- <br />
- @Html.Label("UserEmail")
- @Html.TextBox("UserEmail")
- <br />
- }
- @Html.Label("UserPassword")
- @Html.Password("UserPassword")
- <br />
- @Html.Label("M")
- @Html.RadioButton("M", new { value = "Male" })
- @Html.Label("F")
- @Html.RadioButton("F", new { value = "Female" })
- <br />
For Country selection we can use dropdown and for that we have Html.DropDownList.
Here we have to create a list for the country for that I have added below code outside of BeginForm tag.
Here we have to create a list for the country for that I have added below code outside of BeginForm tag.
- @{
- var MyList = new List<SelectListItem>(){
- new SelectListItem(){Value="1",Text="India"},
- new SelectListItem(){Value="2",Text="UK"}
- };
- }
Now Use this MyList in your Html.DropDownList as follows.
- @Html.Label("UserCountry")
- @Html.DropDownList("UserCountry", MyList)
- <br />


Vikas RaoPosted Feb 26, 2020, 2:10 AM
Thanks Sir
Muhammad AsadPosted Sep 2, 2019, 8:01 AM
How to add left side space in dropdownlist
Humayun Kabir MamunPosted Apr 4, 2016, 5:56 AM
Nice...
kalu singh raoPosted Apr 1, 2016, 12:44 PM
Nice...
Ankur MistryPosted Apr 1, 2016, 2:40 AM
Thank you all
Rajeev PunhaniPosted Apr 1, 2016, 2:37 AM
Helpful article .
Mayank SharmaPosted Apr 1, 2016, 1:38 AM
Very very helpful for all of us Sir. Please keep sharing this type of article.
Nikhil RajputPosted Apr 1, 2016, 1:05 AM
Very Helpful article Sir.
Former memberPosted Apr 1, 2016, 1:04 AM
Good One .....
Rahul LadPosted Apr 1, 2016, 12:41 AM
very useful article
MannanPosted Apr 1, 2016, 12:22 AM
nice share
Hari ShankerPosted Mar 31, 2016, 11:33 PM
nice article