Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am learning MVC and i had find that MVC has full control over the rendered HTML.

Now we are using HTML Helpers in MVC3, where Html.EditorFor stands for HTML <input> elements.
Now i am not understand that if we use this helpers then how mvc has full control over rendered html?
If we use gridview or some other controls in asp.net then they create table, tr, td structure automatically. Same thing happening with Html helpers.

So can any one please explain me what it mean?
Posted

To go with your example of using a textbox.

Html.TextBoxFor(m=>m.Username) creates the stub of
HTML
<input type="text" name="Username" id="Username" />


In order to manipulate this input in MVC 3 (which is pretty much html attributes) is by adding attributes into the applicable parameters

So This

HTML
<input type="text" name="Username" id="Username" class="textbox" /> 


In MVC becomes

C#
@Html.TextBoxFor(m=>m.Username, new { @class = "textbox" });



So for a drop down that looks like the following HTML

HTML
<select name="States" id="States" class="dropdown">
<option>FL</option>
<option>NC</option>
</select>


Becomes this in MVC

C#
SelectListItem[] listStates = new[] {
    new SelectListItem() { Text = "FL"},
    new SelectListItem() { Text = "NC"}
};

@Html.DropDownListFor(m => m.States, listStates, new { @class = "dropdown" })


To specify something to be selected by default all you would have to do is

XML
SelectListItem[] listStates = new[] {
    new SelectListItem() { Text = "FL"},
    new SelectListItem() { Text = "NC", Selected = true}
};


Set Selected to true and that drop down option would become the selected value.

So you see, its not that MVC doesnt give you full control over the html, its that MVC creates a way to think of the html as objects and what not.

Some links that will help build/clarify on what i've briefly explained.

Practical ASP.NET MVC (3) tips[^]

ASP.NET MVC3 Razor With jQuery For Beginners[^]

http://www.asp.net/mvc/mvc3[^]

Specifics on html helpers

http://stephenwalther.com/archive/2009/03/03/chapter-6-understanding-html-helpers.aspx[^]

http://weblogs.asp.net/scottgu/archive/2010/01/10/asp-net-mvc-2-strongly-typed-html-helpers.aspx[^]
 
Share this answer
 
v4
You have full control, since you don't depend on anybody. The html helpers will render pure html elements, everything else is up to you. Those helpers have many overloads to help you emit the code you need. If something is missing, you can make your own overload or helper with a few lines of code.
Btw, if you don't want to use the helpers, than don't use them, write your elements by hand as with php. But than you miss the point: the helper will make your application easier to debug, maintain and develop - and you won't have to give up control.

What control are you missing?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900