Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC2: Impossible to change the name with TextBoxFor?

I want to manually define id and name for textbox like that:

<%: Html.TextBoxFor(model => model.Name, new { @id = "txt1", @name = "txt1" })%>

But only the id is changed, not the name attribute, why?

<input id="txt1" name="Name" type="text" value="">

Thank you!

like image 502
Tuizi Avatar asked May 19 '11 20:05

Tuizi


People also ask

What is the difference between Html TextBox () vs Html TextBoxFor ()?

Ultimately they both produce the same HTML but Html. TextBoxFor() is strongly typed where as Html. TextBox isn't.

What is TextBoxFor?

TextBoxFor represents a single-line input control that allows end-users to enter text.

How do I set TextBoxFor to ReadOnly?

The TextBoxes can be made ReadOnly by setting the HTML ReadOnly attribute using the HtmlAttributes parameter in Html. TextBox and Html. TextBoxFor helper functions. Note: For beginners in ASP.Net MVC, please refer my article ASP.Net MVC Hello World Tutorial with Sample Program example.

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

The MaxLength for TextBoxes is set using the HTML MaxLength attribute using the HtmlAttributes parameter in Html. TextBox and Html. TextBoxFor helper functions.


2 Answers

This is ok:

<%: Html.TextBoxFor(model => model.Name, new { Name = "txt1" })%> 

Do you write "Name" instead of "name"?

Output:

<input  name="txt1" type="text" value=""> 
like image 130
sandro Avatar answered Oct 08 '22 07:10

sandro


Actually you can... just use Name with first letter capitalized instead of name:

@Html.TextBoxFor(model => model.Property, new { Name = "myName" })
like image 35
Leniel Maccaferri Avatar answered Oct 08 '22 08:10

Leniel Maccaferri