我想像这样手动定义文本框的 id 和名称:
<%: Html.TextBoxFor(model => model.Name, new { @id = "txt1", @name = "txt1" })%>
但是只更改了id,而不是name属性,为什么?
<input id="txt1" name="Name" type="text" value="">
谢谢!
这没关系:
<%: Html.TextBoxFor(model => model.Name, new { Name = "txt1" })%>
你写"名称"而不是"名称"吗?
输出:
<input name="txt1" type="text" value="">
其实你可以...只需使用首字母大写的Name
而不是name
:
@Html.TextBoxFor(model => model.Property, new { Name = "myName" })
您不能为此使用强类型的 lambda 版本,您需要使用旧版本
Html.TextBox("txt1",new {@id = "txt1"})
如果仍需要使用 TextBoxFor()
,则可以更改模型上的属性名称,如果您按照建议使用专用的 ViewModels,这应该很容易。但是,我承认这是我并不总是遵循自己的建议。