Friday, 7 August 2015

Understanding HTML Helpers.

An HTML Helper is just a method that returns a string. The string can represent any type of content that you want. For example, you can use HTML Helpers to render standard HTML tags like HTML <input> and <img> tags. You also can use HTML Helpers to render more complex content such as a tab strip or an HTML table of database data.
The ASP.NET MVC framework includes the following set of standard HTML Helpers (this is not a complete list):
  • Html.ActionLink()
  • Html.BeginForm()
  • Html.CheckBox()
  • Html.DropDownList()
  • Html.EndForm()
  • Html.Hidden()
  • Html.ListBox()
  • Html.Password()
  • Html.RadioButton()
  • Html.TextArea()
  • Html.TextBox()
For example, consider the form in Listing 1. This form is rendered with the help of two of the standard HTML Helpers (see Figure 1). This form uses the Html.BeginForm() and Html.TextBox() Helper methods to render a simple HTML form.

DIFFERENCE BETWEEN @Html.TextBox and @Html.TextBoxFor MVC 4 INTERVIEW QUESTOINS

In Razor view engine we use @Html.TextBox and @Html.TextBoxFor for textbox so what is the difference between @Html.TextBox and @Html.TextBoxFor?

Finally both produce the same HTML[/align] but @Html.TextBoxFor() is strongly typed with any model, where as @Html.TextBox  isn't.

For Example:-
@Html.TextBox("Name")
@Html.TextBoxFor(m => m.Name)
both will produce as follows:
<input id="Name" name="Name" type="textbox" />

Generally two things:
a) The typed TextBoxFor will generate your input names for you. This is usually just the property name but for properties of complex types can include an '_' such as 'customer_name'
b) Using the typed TextBoxFor version will allow you to use compile time checking. So if you change you model then you can check whether there are any errors in your views.

How do I specify attributes for a Html.TextBox and Html.TextBoxFor helper while maintaining the value:


You can also use both @Html.TextBox() and @Html.TextBoxFor() with the attributes as  follows:

I am using the Html.TextBox helper to create textboxes. I want to set attributes on the textbox, which I understand is done using the following overload:

@Html.TextBox(string name,object value, object HtmlAttributes)
or
@Html.TextBox("YourProperty","Your Default Value", new { style="width: 30px;" })

Note: I want to maintain the functionality where the HTML helper automatically uses the value from either ViewData or ViewData.

syntax: 

@Html.TextBox( "name", null, new { @class = "css-class" } );

Here "name"  is work as id.On the behalf of this id you can maintaining the value of this Property.


You can pass in additional arguments using the Html.TextBoxFor() Helper similar to what was mentioned in your example : 

@Html.TextBoxFor(model => model.YourProperty, new { style="width: 30px;" });

Here "YourProperty"  is work as id.On the behalf of this id you can maintaining the value of this Property.

@Html.TextBoxFor(model => model.FirstNmae, new { style="width: 30px;" });
@Html.TextBoxFor(model => model.LastNmae, new { style="width: 30px;" });
@Html.TextBoxFor(model => model.EailId, new { style="width: 30px;" });

from this type we can use @Html.TextBoxFor().but FirstNmae,LastNmae,EailId this is strongly type with your model.








No comments:

Post a Comment