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
a little bit "unpretty"=), try:
@Html.TextBoxFor(x => x.Data).ToString().Replace("Data", "NewData")
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.
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" />
It is call Microsoft GOTCHA
Use the name in caps, like this
@Html.TextBoxFor(m => m.Reply.Answer, new {@Name="Whatyouwant"})
EditorFor has an overload where you can supply the name
attribute as a parameter:
@Html.EditorFor(expression, null, name)
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"))