ASP.NET MVC 3: Override “name” attribute with TextBoxFor

前端 未结 11 1476
臣服心动
臣服心动 2020-11-28 05:41

Is it possible when using Html.TextBoxFor to override the name attribute?

I have tried with no success. I need to use TextBoxFor to get client side vali

相关标签:
11条回答
  • 2020-11-28 06:03

    a little bit "unpretty"=), try:

    @Html.TextBoxFor(x => x.Data).ToString().Replace("Data", "NewData")
    
    0 讨论(0)
  • 2020-11-28 06:04

    Are you asking this because you want to apply a prefix to the name? If so, you can do this by setting ViewData.TemplateInfo.HtmlFieldPrefix in your Controller.

    I learnt a lot about this stuff from Brad Wilson's blog.

    0 讨论(0)
  • 2020-11-28 06:05

    For this example, I was disabling form fields based on permissions, but still showing them. I had a hidden field to send the value to the controller, but wanted a different field name in the EditorFor. First param after model value represents the "name" property, second is the new name.

    @Html.EditorFor(m => m.UserName, "name", "UserNameDisabled", new { htmlAttributes = new { @class = "form-control", @disabled = "disabled"} });
    

    Results in:

    <input class="form-control text-box single-line" disabled="disabled" id="UserNameDisabled" name="UserNameDisabled" type="text" value="someEnteredValue" /> 
    
    0 讨论(0)
  • 2020-11-28 06:08

    It is call Microsoft GOTCHA

    Use the name in caps, like this

    @Html.TextBoxFor(m => m.Reply.Answer, new {@Name="Whatyouwant"})
    
    0 讨论(0)
  • 2020-11-28 06:10

    EditorFor has an overload where you can supply the name attribute as a parameter:

     @Html.EditorFor(expression, null, name)
    
    0 讨论(0)
  • 2020-11-28 06:10

    ben's answer got me what I was looking for except you need to wrap in in Html.Raw

    @Html.Raw(Html.TextBoxFor(x => x.Data).ToString().Replace("Data", "NewData"))
    
    0 讨论(0)
提交回复
热议问题