MVC2: Impossible to change the name with TextBoxFo

2019-02-17 08:45发布

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!

4条回答
时光不老,我们不散
2楼-- · 2019-02-17 08:49

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楼-- · 2019-02-17 08:58

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

@Html.TextBoxFor(model => model.Property, new { Name = "myName" })
查看更多
不美不萌又怎样
4楼-- · 2019-02-17 09:12

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"})
查看更多
冷血范
5楼-- · 2019-02-17 09:14

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.

查看更多
登录 后发表回答