Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET MVC
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 16-Jan-13 23:24pm
VIPR@T2.8K
Rate this: bad
good
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">
<option>FL</option>
<option>NC</option>
</select>
 
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[^]
 
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[^]
  Permalink  
v4
Rate this: bad
good
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?
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,649
1 Sergey Alexandrovich Kryukov 6,270
2 CPallini 5,190
3 George Jonsson 3,574
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2014
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