Search This Blog

2014-01-06

MVC-Basic to Advanced:Part 1-Create a New Project in MVC & Introduction to HTML Helper

Hello Guys, Its a long time I was not in touch with my blog due to some personal constraint.Lets start it once again.... In this MVC series, I have a plan to contribute from a basic to advanced modules on MVC. Lets start with a simple MVC solution..... Step 1: Open the VS2012, Create a New Project 'MyMvcApplication' as below

Step 2:Select 'InternetApplication' From the template and 'Razor' as a view engine.Click OK.

Step 3.Modify the Index.cshtml as below so that it looks like simple and run it.

Step 4. Once Run, It should looks like below...


HTML Helper : HTML helpers are an easy way to render the appropriate HTML markup in a view and returns an HTML-encoded string of the type MvcHtmlString(i.e already HTML-encoded) and and therefore should not be encoded again. Example : In the Index.cshtml, you can add HTML Helpers @Html.TextBox and @Html.CheckBox like below .
@{
ViewBag.Title = "Home Page";
}
@section featured {
<section class="featured">
<div class="content-wrapper">
<hgroup class="title">
<h1>@ViewBag.Title.</h1>
<h2>@ViewBag.Message</h2>
@Html.TextBox("myTextbox")
@Html.CheckBox("myCheckbox")
</hgroup>
</div>
</section>
}

Standard HTML Helpers :

Form Extensions
  • Html.BeginForm()
  • Html.BeginRouteForm()
  • Html.EndForm()

Input Extensions
  • Html.CheckBox()
  • Html.Hidden()
  • Html.Password()
  • Html.RadioButton()
  • Html.TextBox()
TextArea Extensions
  • Html.TextArea()
Label Extensions
  • Html.Label()
Link Extensions
  • Html.ActionLink()
  • Html.RouteLink()
Select Extensions
  • Html.DropDownList()
  • Html.ListBox() 
Validation Extensions
  • Html.Validate()
  • Html.ValidationMessage()
  • Html.ValidationSummary()
The above HTML Helper can be made as a strongly typed(except the Form Extension) by addling a 'for' as a suffix(like Html.TextBoxFor(model => model.FirstName)) and use use lambda expressions when referring to the model that has been passed to the view.

No comments: