MVC2: imposible cambiar el nombre con el cuadro de texto para?
-
16-11-2019 - |
Pregunta
Quiero definir manualmente la identificación y el nombre para el cuadro de texto así:
<%: Html.TextBoxFor(model => model.Name, new { @id = "txt1", @name = "txt1" })%>
Pero solo se cambia la identificación, no el atributo de nombre, ¿por qué?
<input id="txt1" name="Name" type="text" value="">
¡Gracias!
Solución
You can't use the strongly typed lambda version for this, you'd need to use the older version
Html.TextBox("txt1",new {@id = "txt1"})
Otros consejos
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="">
Actually you can... just use Name
with first letter capitalized instead of name
:
@Html.TextBoxFor(model => model.Property, new { Name = "myName" })
If you still need to use TextBoxFor()
, you can change the name of the property on your model, which should be easy if you're using dedicated ViewModels as is recommended. However I admit it's a recommendation I don't always follow myself.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow