覆盖Html.EditorFor的"id"属性 - 不起作用

Pat*_*tty 5 html asp.net-mvc-3

试图覆盖Html.TextBoxFor(MVC 3)的"id"属性,使它看起来像:

<input type="text" name="Password" id="@idPasswordTextBox" value="@Model.Password" />
Run Code Online (Sandbox Code Playgroud)

其中"idPasswordTextBox"定义为: string idPasswordTextBox = "passwordText_"+@Model.Key;在同一个cshtml文件中.

如果我用作:

<input type="text" name="Password" id="@idPasswordTextBox" value="@Model.Password" />
Run Code Online (Sandbox Code Playgroud)

但如果我这样做,就无法工作:

@Html.TextBoxFor(model => model.Password, new { id = "@idPasswordTextBox" })
Run Code Online (Sandbox Code Playgroud)

看起来"id"属性搞砸了.我错过了什么?有人可以帮忙吗?我是ASP.net的新蜜蜂.

提前致谢.

Jas*_*son 5

对不起,我应该仔细看看.您不希望TextBoxFor方法中的@idPasswordTextBox引用.这是在服务器上运行的,因此当您在名称周围加上引号时,它将被视为文字字符串.删除引号,并删除id前面的@符号,它将起作用.

始终记住服务器上运行的内容以及客户端上运行的内容非常重要.

@Html.TextBoxFor(model => model.Password, new { id = @idPasswordTextBox })
Run Code Online (Sandbox Code Playgroud)