- A form is a container for input elements: buttons, checkboxes, and more. Input elements in a form enable a user to enter information into a page and submit information to a server.
- There are two main attributes of a form tag: the
action
and themethod
action
attribute tells a web browserwhere
to send the information.method
attribute tells the browser whether to use HTTP POST or HTTP GET when sending the information. Default value is HTTP GET
Example:
<form action='/Student/Create' method="post">
<input name="fullname" type="text" />
<input type="submit" value="Submit" />
</form>
These are methods to make views easy to author. Most of them output HTML markup.
Some HTML Helpers:
- BeginForm
This helps to build a robust form. Example:
@using (Html.BeginForm("Create", "Student", FormMethod.Get)) {
<input name="fullname" type="text" />
<input type="submit" value="Submit" />
}
- Validation Helpers
- Html.ValidationSummary
This displays unordered list of all validation errors in theModelState
dicitionary. - @Html.ValidationMessage
When an error exists for a particular field in the
ModelState
dicitionary, you can useValidationMessage
helper to display that message.
- Html.ValidationSummary
- Input HTML Helpers
- @Html.TextBox
TextBox
helper renders an input tag with the type attribute set totext
. Example:
@Html.TextBox("fullname", Model.FullName)
results<input id="fullname" name="fullname" type="text" />
- @Html.TextArea
This will render<textarea>
element for multiline entry. - @Html.Label
This return<label />
element using the string parameter to determine rendered text and for attribute value. - @Html.DropDownList and @Html.ListBox
Both of these return<select />
element.DropDownList
allows you to select single element whileListbox
allows you to select multiple values. - Other Input helpers:
@Html.Hidden
,@Htm.RadioButton
,@Html.Password
,@Html.Checkbox
.
- @Html.TextBox
- Rendering Helpers
These helpers produce links to other resources inside an application. Examples:@Html.ActionLink
and@Html.RouteLink
- Strongly Typed Helpers
with strongly typed helpers, you pass a lambda expression to specify a model property for rendering. They have same name as other helpers but withFor
suffix. Examples:@Html.TextBoxFor
,@Html.LabelFor
,@Html.DropDownListFor
etc.