Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the Source Code for Html.TextBoxFor

I am currently browsing the source code of asp.net mvc 3 I just downloaded from codeplex. I'm particularly interested in seeing the source code of strongly typed Helper methods like TextBoxFor and DropDownListFor but failed to locate them in InputExtensions.cs and elsewhere.

Would someone point out where I can find required code? My purpose is to see how these helper methods add Html 5's data-* attributes to different input elements on the form.

like image 727
Muhammad Adeel Zahid Avatar asked May 11 '11 09:05

Muhammad Adeel Zahid


People also ask

How do I use TextBoxFor in HTML?

The HtmlHelper class includes two extension methods TextBox() and TextBoxFor<TModel, TProperty>() that renders the HTML textbox control <input type="text"> in the razor view. It is recommended to use the generic TextBoxFor<TModel, TProperty>() method, which is less error prons and performs fast.

How do I set the MaxLength for HTML TextBoxFor in MVC?

The TextBox for the Name value is created using Html. TextBoxFor function while the TextBox for the Mobile Number value is created using Html. TextBox helper function. The MaxLength of both the TextBoxes is set using the HTML MaxLength attribute using the HtmlAttributes parameter in Html.

What is the difference between HTML TextBox and HTML TextBoxFor?

IMO the main difference is that Textbox is not strongly typed. TextboxFor take a lambda as a parameter that tell the helper the with element of the model to use in a typed view. You can do the same things with both, but you should use typed views and TextboxFor when possible.


2 Answers

You don't need to download the source code or use reflector extensions.

If you "go to definition" (F12), you should see the file metadata, which is the summarized method declarations. From there, hover over the tab, and you should see the (local) file path where the method came from, which also corresponds to the namespace. With that, you'll be able to look it up from the MVC source code on Codeplex:

http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/5cb74eb3b2f3#src/System.Web.Mvc/Html/InputExtensions.cs

like image 175
drzaus Avatar answered Nov 25 '22 06:11

drzaus


Get reflector (version 6. should be somewhere available for free), open System.Web.Mvc.dll and search for InputExtensions

or

Get mvc source code, open in visual studio, and search for InputExtensions

edit: misread the question..

I guess you are looking for InputExtensions.cs line: 371

tagBuilder.MergeAttributes(htmlHelper.GetUnobtrusiveValidationAttributes(name, metadata));
like image 44
Lukáš Novotný Avatar answered Nov 25 '22 05:11

Lukáš Novotný