Html MVC2:无法使用TextBoxFor更改名称?
我想手动定义文本框的id和名称,如下所示:Html MVC2:无法使用TextBoxFor更改名称?,html,asp.net,asp.net-mvc,asp.net-mvc-2,html.textboxfor,Html,Asp.net,Asp.net Mvc,Asp.net Mvc 2,Html.textboxfor,我想手动定义文本框的id和名称,如下所示: <%: Html.TextBoxFor(model => model.Name, new { @id = "txt1", @name = "txt1" })%> model.Name,新的{@id=“txt1”,@Name=“txt1”})%> 但是只更改了id,而没有更改name属性,为什么 <input id="txt1" name="Name" type="text" value=""> 谢谢大家! 您不能
<%: Html.TextBoxFor(model => model.Name, new { @id = "txt1", @name = "txt1" })%>
model.Name,新的{@id=“txt1”,@Name=“txt1”})%>
但是只更改了id,而没有更改name属性,为什么
<input id="txt1" name="Name" type="text" value="">
谢谢大家! 您不能为此使用强类型lambda版本,您需要使用旧版本
Html.TextBox("txt1",new {@id = "txt1"})
如果仍然需要使用
TextBoxFor()
,可以更改模型上属性的名称,如果按照建议使用专用的ViewModels,这应该很容易。不过,我承认这是一个我并不总是遵循自己的建议。这没关系:
<%: Html.TextBoxFor(model => model.Name, new { Name = "txt1" })%>
model.Name,新的{Name=“txt1”})%>
你写的是“姓名”而不是“姓名”吗
输出:
<input name="txt1" type="text" value="">
实际上你可以。。。只需使用首字母大写的
Name
,而不是Name
:
@Html.TextBoxFor(model => model.Property, new { Name = "myName" })
使用ASP.NET MVC 3,“@Html.TextBoxFor(m=>m.UserName,new{Name=“user_Name”})”生成“”(名称和名称属性)。它也在MVC4中工作,同时呈现MVC2中确认的
。这是一个重复的解决方案,但它不是“解决方案”。例如,TextBoxFor()包含不引人注目的验证属性,具体取决于修饰给定属性的属性以及IClientValidable属性。TextBox()没有。@isaaclopis:检查我的答案,并感到惊讶…:)大写字母能做什么!哇,我有这个问题很久了,name
不起作用,但是name
起作用了!它甚至适用于Id
。