JavaEar 专注于收集分享传播有价值的技术资料

使用Html.TextBoxFor时,将模型的名称添加为id的前缀 (When using Html.TextBoxFor, add the name of the model as prefix to id)

I have an asp mvc 3 project When I use

@Html.TextBoxFor(model => model.Name)

the TexBoxFor render as

<input id="Name" name="Name" type="text" value="" />

my model is "UserModel" :

[DisplayName("Name")]
public string Name{ get; set; }

Is there a way to add to the name of the model as prefix in the id? maybe some attribute?

I want that the text box will render as

<input id="UserName" name="UserName" type="text" value="" />

6个回答

    最佳答案

  1. 英文原文

    If you don't want to render the input as described in your model, you probably don't want to be using the Html.TextBoxFor helper.

    Try Html.TextBox instead. You can provide it with the exact values you're looking for:

    @Html.TextBox("UserName", Model.Name, new { id = "UserName" })
    

    Don't forget you can also forget the helpers and use plain old html:

    <input id="UserName" name="UserName" type="text" value="@Model.Name" />
    

    Warning: When using either of these methods and the default model binder, the value of this input will not be bound back to your model correctly when submitting back the data.


    中文翻译

    如果您不想按模型中的描述呈现输入,则可能不希望使用 Html.TextBoxFor 帮助程序。

    尝试 Html.TextBox 。您可以为其提供您正在寻找的确切值:

      @ Html.TextBox(" UserName" ,Model.Name,new {id =" UserName" })
     

    不要忘记你也可以忘记助手并使用普通的旧HTML:

     &lt; input id =" UserName" name =" UserName" type =" text" value =" @ Model.Name" /&gt;
     

    警告:使用这些方法和默认模型绑定器时,在提交数据时,此输入的值不会正确绑定到模型。

    If you don't want to render the input as described in your model, you probably don't want to be using the Html.TextBoxFor helper.

    Try Html.TextBox instead. You can provide it with the exact values you're looking for:

    @Html.TextBox("UserName", Model.Name, new { id = "UserName" })
    

    Don't forget you can also forget the helpers and use plain old html:

    <input id="UserName" name="UserName" type="text" value="@Model.Name" />
    

    Warning: When using either of these methods and the default model binder, the value of this input will not be bound back to your model correctly when submitting back the data.

    如果您不想按模型中的描述呈现输入,则可能不希望使用 Html.TextBoxFor 帮助程序。

    尝试 Html.TextBox 。您可以为其提供您正在寻找的确切值:

      @ Html.TextBox(" UserName" ,Model.Name,new {id =" UserName" })
     

    不要忘记你也可以忘记助手并使用普通的旧HTML:

     &lt; input id =" UserName" name =" UserName" type =" text" value =" @ Model.Name" /&gt;
     

    警告:使用这些方法和默认模型绑定器时,在提交数据时,此输入的值不会正确绑定到模型。

  2. 参考答案2
  3. The TextBoxFor helper uses the lambda expression that is passed as first argument to calculate the name of the input field. So if you want the generated input field to be called UserName, simply rename the property in your view model:

    [DisplayName("Name")]
    public string UserName { get; set; }
    
  4. 参考答案3
  5. @Html.TextBoxFor(model => model.attr, new { Name = "txt1" })
    

    Just Use "Name" instead of "name"

  6. 参考答案4
  7. You should change your DisplayName attribute from "Name" to "UserName".

    [DisplayName("UserName")]
    public string Name{ get; set; }
    

    If you do that, you'll get what you're asking for.

  8. 参考答案5
  9. If I understood your question correctly, you want to be able to prefix a string onto the Model's property name.

    Therefore if you are in the view 'Account', you will get

    AccountName
    

    whilst in the view 'Test', you will get

    TestName
    

    If that is the case, I think your best bet is overriding the PartialFor extension method, similarly as is done here:

    ASP.NET MVC partial views: input name prefixes

    In this example, it will render

    Account.Name and Test.Name
    
  10. 参考答案6
  11. You can write:

    @Html.TextBoxFor(model => model.Name, new { @id= "UserName",@Name="UserName" })