18 April 2014

Helper Classes

There are several Helper classes in MVC frame work. 

1. Form Helper- Radio buttons, textboxes, list boxes etc.
 

2.URL helpers
 

3.HTML helpers- Encode, Decode, Attribute Encode, and Render Partial.

Form Helper Class

Some of the commonly used HTML helpers are 

* ActionLink: Link to an action method

@Html.ActionLink ("About MVC", "About", "Home")

When we click on the link 'About MVC' it links to the 'About' method in the 'Home Controller'.

*Textbox 

Name: @Html.TextBox ("name")

*Begin Form- Marks the start of a form and links to the action method that renders the form. 

@using (Html.BeginForm ("Add Details", "admin", Form Method.Post, new {enctype = "multipart/form-data" }))
{
-----Form Content-----
}

When the from submits the data goes to 'Add Details' action method in the 'admin controller'.

*Dropdown List- Renders a drop-down list.
 

The code for the 'Countries' Action method
 
Public ActionResult Countries ()
        {
            List countryList = new List ();
countryList.Add ("India");
countryList.Add ("Pakistan");
countryList.Add ("USA");
countryList.Add ("Afghanistan");
countryList.Add ("China");
countryList.Add ("France");
countryList.Add ("Japan");
            ViewBag.countries = new Select List (countryList);
Return View ();
        }

The View page of 'Countries' will contain

Countries: @Html.DropDownList ("countries ")

* RadioButton 

Select your Country:@Html.RadioButton ("country", "India", true) India
          @Html.RadioButton ("country", "Pakistan", false) Pakistan
          @Html.RadioButton ("country", "USA", false) USA
          @Html.RadioButton ("country", "Afghanistan", false) Afghanistan
          @Html.RadioButton ("country", "China", false) China
          @Html.RadioButton ("country", "France", false) France
          @Html.RadioButton ("country", "Japan", false) Japan

* CheckBox: @Html.CheckBox ("country")


URL Helper classes

URL Helper class provide the following methods.

1. Action- Generates a URL that maps to an action method.

Overload Lists.

*Action (String)

*Action(String, Object)

*Action(String, String)

*Action(String, RouteValueDictionary)

*Action(String, String, Object)

*Action(String, String, RouteValueDictionary)

*Action(String, String, Object, String)

*Action(String, String, RouteValueDictionary, String, String)

Syntax for Action(String)
public string Action( string actionName)

2.RouteUrl- Generates a URL that maps to a route.

Overload Lists.

*RouteUrl(Object)

*RouteUrl(String)

*RouteUrl(RouteValueDictionary)

*RouteUrl(String, Object)

*RouteUrl(String, RouteValueDictionary)

*RouteUrl(String, Object, String)

*RouteUrl(String, RouteValueDictionary, String, String)

Syntax for RouteUrl(Object) 

public string RouteUrl( Object routeValues )

3.Content-Generates a URL path to a resource, based on the virtual (relative) path of the resource. 

Syntax
public string Content( string contentPath)

4.Encode- This method encodes special characters in the specified URL into character-entity equivalents. 

Syntax
public string Encode( string URL )

HTML Helper Classes

MVC include HTML Helpers like links and HTML form elements.

Links include HTML.ActionLink() helper.

Razor syntax of ActionLink is
@Html.ActionLink("About MVC", "About", "Home")

The Html.ActionLink() helper above, outputs the following HTML:
<a href="/Home/About">About MVC</a>

HTML Form Elements

*Html.BeginForm()

*Html.CheckBox()

*Html.DropDownList()

*Html.EndForm()

*Html.Hidden()

*Html.ListBox()

*Html.Password()

*Html.RadioButton()

*Html.TextArea()

*Html.TextBox()

For an Instance is below 

@Html.Validation Summary ("Create was unsuccessful. Please correct the errors and try again.")
@using (Html.BeginForm ()) {
<p>
<label for="FirstName">First Name :< /label>
@Html.TextBox ("FirstName")
@Html.ValidationMessage ("FirstName", "*")
</p>
<p>
<label for="LastName">Last Name :< /label>
@Html.TextBox ("LastName")
@Html.ValidationMessage ("LastName", "*")
</p>
<p>
<label for="gender">Gender :< /label>
@Html.RadioButton ("gender", "Male", true) Male
@Html.RadioButton ("gender", "Female", false) Female
</p>      
<p>
<label for="Password">Password :< /label>
@Html.Password ("Password")
@Html.ValidationMessage ("Password", "*")
</p>
<p>
<label for="Password">Confirm Password :< /label>
@Html.Password ("Confirm Password")
@Html.ValidationMessage ("Confirm Password", "*")
</p>
<p>
<label for="Profile">Profile :< /label>
@Html.TextArea ("Profile", new {cols = 60, rows = 10})
</p>
<p>
@Html.CheckBox ("Receive Newsletter")
<label for="termsAndconditions" style="display: inline">I Agree</label>
</p>
<p>
<input type="submit" value="Register" />
</p>
}

 







No comments:

Post a Comment