-1

Is there any Html.TextBoxFor overload implementation which allows value formatting?

I know about EditorFor and DataFormatString but it is not what I am looking for.

SiberianGuy
  • 24,674
  • 56
  • 152
  • 266

2 Answers2

1

Why not use an EditorTemplate? Of course, if you're going to use different ways of formatting the same objects you're going to have to use the template name in the call to EditorFor() (so it'll look like EditorFor(object, "TemplateName").

Another option is to add your own HTML Helper. The MVC source code is open so you can download the RTM sources for 2/3 and look at how Html.TextBoxFor() is implemented.

I think EditorFor() is the correct solution to this problem. Why don't you want to use it?

Cymen
  • 14,079
  • 4
  • 52
  • 72
  • It causes some duplication. I have htmlAttributes set to new {@class="someClass"} for all my textboxes and I don't want to repeat it in all EditorTemplates – SiberianGuy Sep 15 '11 at 15:55
  • EditorTemplates should be the only place you need to add `someClass`. Keep 'em in there and all of your inline will be much cleaner. – Chase Florell Sep 15 '11 at 15:56
  • I've done both approaches and the `EditorFor()` is cleaner. Writing HtmlHelper extensions works great but it takes more time and gets tricky (does your helper work with the new client side validation, etc). – Cymen Sep 15 '11 at 16:32
  • I prefer to use Razor Declarative Helpers instead of Templates. And here I just need to format value for HtmlTextBoxFor. – SiberianGuy Sep 15 '11 at 16:38
  • I did a quick google on "razor declarative helpers" and it looks like this is per view, right? For one offs that seems fine but for reusable things that doesn't sound good. Or am I missing something? Reading this: http://stackoverflow.com/questions/4451287/razor-declarative-html-helpers – Cymen Sep 15 '11 at 16:44
  • @Cymen, you can put helper into App_Code folder and then it becomes visible globally – SiberianGuy Sep 15 '11 at 18:46
  • @Idsa: That is rather ugly. Hopefully that is fixed in MVC 4. I'll try that method too though. Thanks! – Cymen Sep 15 '11 at 21:40
-2

It is possible in ASP.NET MVC 4: http://forums.asp.net/p/1720963/4599128.aspx/1?Html+TextBoxFor+value+formatting

SiberianGuy
  • 24,674
  • 56
  • 152
  • 266
  • Link only answers are discouraged. You should summarise the contents of that post in this question. – Liam Feb 26 '14 at 13:02