views:

291

answers:

3

I am playing around with the ASP.NET MVC Html.Helpers and I noticed that say for instance:

Html.Textbox("test");

will render the name attribute to "name=test" and the id tag will be "id=test"

But when I do this:

<%= Html.TextBox("go", null, new { @name = "test2", @id = "test2", @class = "test2" })%>

id will be "id=test2" but name will be "name=go"

Why does it not get overridden?

I also don't really still understand what the name tag actually does. I don't think I ever even used.

P.S

I am aware that "name" and "id" probably don't need to be escaped only "class" does since it is a keyword but I just do it for all of them just so I don't forget to do it or have to even remember if something is a keyword or not.

+1  A: 

ID is unique identifier in DOM tree, name is identifier within form, and it doesn't need to be unique, name is used after submitting the form.

Yossarian
+2  A: 

The name attribute is used when accessing that form element's value on the server side. Like so :

string val = Request.Name["go"];

As for specifying the name attribute, well that's what the first parameter of the Html.TextBox method is there for.

çağdaş
ah ok can't believe that one flew over my head. The parameter name is called "name" can't believe I could not connect the dots.So then I guess I should be asking why do they want you to specify the name and not the id?I am building my own custom html helper should I just do what they just ask them to specify the name?
chobo2
Well, I guess it's because the name attribute is an attribute that belongs to the input element(s). Whereas the id attriute is a standard attribute that can be given to any element on page.
çağdaş
+1  A: 

Because it is a "Helper" helping you not to type the name in the 90% of the cases when you create a textbox.

You are free to not use them and just type:

<input type="textbox" 
       name="theNameIReallyWant" 
       value="<%= model.theNameOfThePropertyInTheModel %>">

or better yet, create your own TextBoxWithCustomName helper

Eduardo Molteni