What is Html helper control and what is difference between Untyped html helper control and typed html helper control? [Resolved]

Posted by Kumarkrishna184 under ASP.NET MVC on 12/1/2015 | Points: 10 | Views : 1001 | Status : [Member] | Replies : 3
What is html helper control and what is difference between untyped html helper control and typed html helper control?

Thanks and Regards,
Krishna Kumar



Responses

Posted by: Rajnilari2015 on: 12/1/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
What is html helper control?
---------------------------------


In Classic ASP.NET, we have the web form controls like
<asp:button>, <asp:textbox>,<asp:lable>,<asp:datagrid> etc
After rending at the client side, these controls gets transform to standard HTML controls like
<input type="button">,<input type="text">,<span>,<table>
respectively.

Unlike Classic ASP.NET, Asp.net MVC does not provide any direct web form controls.However, they does so by providing the HTML Helper which are more lightweight in nature as compared to Classic ASP.NET web form controls.

Let us look at an example to get a more clarity on the picture

Suppose we need to have a Textbox control design in Classic ASP.NET. In that case we write

<asp:textbox id="myTxtBox" name="myTxtBox" runat="server" value="default text"/>


This get render at the browser to

<input type="text" id="myTxtBox" name="myTxtBox" value="default text""/>


The same can be achieve in Asp.net MVC by using the HTML Helper as shown below

@Html.TextBox("myTxtBox", "default text"")


From the above we can infer that, HTML Helper is like a method that returns a HTML string.

But unlike the Classic ASP.NET web form controls, HTML Helper does not have events associated with them and also does not have any view state.

Asp.net MVC provides built-in HTML Helper's, Templated Helpers. If the existing HTML Helper's does not meet the requirment, one can create Custom Html Helpers too.


What is Strongly Typed HTML Helpers?
---------------------------------------


These kind of Html Helpers comes into use when the HTML elements are created based on model properties in a strongly types view. What the statement means can be better demonstrated by an example -

Let us create a model say Employee

namespace DemoModel
{
public class Employee
{
public int EmpID { get; set; }
public string EmpName { get; set; }
}
}


Now let us create a strongly typed Employee using the Html helpers as under

@model DemoModel.Employee
@using(@Html.BeginForm()){

<div>
@Html.DisplayFor(m => m.EmpID)
@Html.TextBoxFor(m=>m.EmpID)
</div>

<div>
@Html.DisplayFor(m => m.EmpName)
@Html.TextBoxFor(m=>m.EmpName)
</div>
}


From the above code snippet, it is clear that the strongly typed HTML helpers work on lambda expression where the model object is passed as a value to lambda expression and we can select the field or property from model object to be used to set the id, name and value attributes of the HTML helper. The above code snippet will be render to

<div>
<span id="EmpID" name="EmpID"> { EmpID value(s) }</span>
<input id="EmpID" name="EmpID" type="text" value="{ EmpID value(s) }" />
</div>
<div>
<span id="EmpName" name="EmpName"> { EmpName value(s) }</span>
<input id="EmpName" name="EmpName" type="text" value="{ EmpName value(s) }" />
</div>


What is UnTyped Typed HTML Helper?
---------------------------------------


We have already seen what a Strongly Typed HTML Helpers are. UnTyped Typed HTML Helpers are those HTML Helpers that are not bound to any Model data.

E.g. @Html.RadioButton("myRDBtn", "val", true) 


This will be render to

<input type="radio" id="myRDBtn" name="myRDBtn" checked="checked" value="val" />


This radio button is not tied to any model and hence become an UnTyped Typed HTML Helper.

Hope this helps.

Thanks.

--
Thanks & Regards,
RNA Team

Kumarkrishna184, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kumarkrishna184 on: 12/1/2015 [Member] Starter | Points: 25

Up
0
Down
Thanks alot, this is very helpful to me,now i am very clear about html helper.

Thanks and Regards,
Krishna Kumar

Kumarkrishna184, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 12/1/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
@Kumarkrishna184 Sir, glad that it has helped you.

--
Thanks & Regards,
RNA Team

Kumarkrishna184, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response