Click here to Skip to main content
13,399,624 members (51,254 online)
Rate this:
Please Sign up or sign in to vote.
See more:

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 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 17-Jan-13 0:24am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

To go with your example of using a textbox.

Html.TextBoxFor(m=>m.Username) creates the stub of
<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

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

In MVC becomes

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

So for a drop down that looks like the following HTML

<select name="States" id="States" class="dropdown">

Becomes this in MVC

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

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[^][^]

Specifics on html helpers[^][^]
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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?

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.180218.2 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100