Z 8 ) MVC HTMLHELPER METHOD AND ACTIONLINK (how to use css ,atrributes,style in css)
HTML HELPER METHODS IN ASP.NET MVC 5
In ASP.NET MVC Framework, helper methods:
Ø Are extension methods to the Html Helper class, can be called only from views.
Ø An HTML helper is a method that is used to render html content in a view.
Ø Simplifies the process of creating a view.
Ø Allows generating HTML markup that you can reuse across the Web application.
Some of the commonly used helper methods while developing an MVC application are as follows:
Ø Html.ActionLink()
Ø Html.BeginForm() and Html.EndForm()
Ø Html.Label()
Ø Html.TextBox()
Ø Html.TextArea()
Ø Html.Password()
Ø Html.CheckBox()
Html.ActionLink()
Html.ActionLink() helper method allows you to generate a hyperlink that points to an action method of a controller class.
The general syntax of the Html.ActionLink() helper method is as follows:
@Html.ActionLink(<link_text>,<action_method>,<optional_controller>)
Use two types :-
@Html.ActionLink("Go to About Page","About") // Name and Viewname
<a href="@Url.Action("About")">Go to About Page</a>
HTML HELPER METHODS IN ASP.NET MVC 5
An HTML helper is a method that is used to render html content in a view
Some of the commonly used helper methods while developing an MVC application are as follows:
Ø Html.ActionLink()
Ø Html.BeginForm() and Html.EndForm()
Ø Html.Label()
Ø Html.TextBox()
Ø Html.TextArea()
Ø Html.Password()
Ø Html.CheckBox()
· To create the html for a textbox with id = “fullName” and name = “fullName”
o <input type=”text” name = “fullName” id = “fullName”>
OR
· We can use the “TextBox” Html helper class method.
o @Html.TextBox(“fullName”)
· There are several overloaded versions. To set a value, along with name of a textbox
o @Html.TextBox(“fullName”,”Adil”)
To set HTML attributes, use the following overloaded version. Note that: We are passing HTML attributes (style and title) as an anonymous type.

· We can use external style sheet to give the style to the textbox.
· Applying bootstrap class to textbox.
· If you want to use attributes that are reserved in Csharp programming language like class, readonly etc. Then you have to use @ symbol before the name of attribute.
HTML HELPER METHODS IN ASP.NET MVC 5
Some of the commonly used helper methods while developing an MVC application are as follows:
Ø Html.ActionLink()
Ø Html.BeginForm() and Html.EndForm()
Ø Html.Label()
Ø Html.TextBox()
Ø Html.Password()
Ø Html.RadioButton()
Ø Html.DropDownList()
Ø Html.TextArea()
Ø Html.CheckBox()
Ø Html.hidden()
· IS IT COMPULSORY TO USE HTML HELPERS?
o No, you can type the required html, but using html helpers will greatly reduce the amount of HTML markup that we have to write in a view. Views should be as simple as possible.
Programme ::
Controller :-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace A2_HtmlHelpe_Method_Atrribute_Css_Boostrap_.Controllers
{
public class EmplyoeeController : Controller
{
public ActionResult Index()
{
// all code inside the index view
// * PART 1 *
return View();
}
public ActionResult Index2()
{
// * PART 2 * All Html HElper Using Create boostrap Form
return View();
}
}
}
View :-
Index View :-
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<input type="text" name="name" value="" /> @*Normal Html Code*@
<br />
<br />
@Html.TextBox("txtname")
<br />
<br />
@Html.TextBox("txtname", "Sourabh")
<br />
<br />
@Html.TextBox("txtname", null, new { style = "background:yellow; color:red; font - weight:bold;", title = "Please Enter Your Name" })
<br />
<br />
@Html.TextBox("txtname", "Sourabh", new { @class = "form-control", @readonly = true })
<br />
<br />
@Html.TextBox("txtname", "Sourabh", new { @class = "TextBoxStyle" })
@*Goto to content -- Site.Css -- Add css*@
@*.TextBoxStyle{
background:lightgreen;
color:red;
font-weight:bolder;
}*@
Index 2 view:-
@{
ViewBag.Title = "Index2";
}
<h2>Index2</h2>
@{
Html.BeginForm("Index", "Emplyoee");
}
@Html.Label("Name") @Html.TextBox("UserName","",new {@class="form-control"})
<br />
@Html.Label("Password") @Html.Password("pass", "", new { @class = "form-control" })
<br />
@Html.Label("Gender") @Html.Label("Male") @Html.RadioButton("Gender", "Male", true) @*Already check using booleans True*@
@Html.Label("Female") @Html.RadioButton("Gender", "Female")
<br />
@Html.Label("City") @Html.DropDownList("citylist", new SelectList(new[] { "Nagpur", "Pune", "Dhule", "Nashik", "Bhandara" }), "Select",new {@class="form-control" })
<br />
@Html.Label("Message") @Html.TextArea("messagearea", "", 5, 10, new { @class = "form-control" }) @*Rows : 5, Colums :10*@
<br />
@Html.Label("Terms & Condition") @Html.CheckBox("checkbox", true) @*Already check using booleans True*@
<br />
@Html.Hidden("Id")
<br />
<input type="submit" value="Submit" class="form-control"/>
@{
Html.EndForm();
}
Comments
Post a Comment