MVC2: Impossible to change the name with TextBoxFor?

僤鯓⒐⒋嵵緔 提交于 2019-12-01 02:18:12

问题


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!


回答1:


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"})



回答2:


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=""> 



回答3:


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

@Html.TextBoxFor(model => model.Property, new { Name = "myName" })



回答4:


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.



来源:https://stackoverflow.com/questions/6064363/mvc2-impossible-to-change-the-name-with-textboxfor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!