1

Okay, I have been looking for a resolution for this simple task all day.

I have an mvc model class which has a BIC property and a NationalNumber property. Bother are string values. I want to use a DisplayFor helper to render the property on the view BUT, with a formatting applied.

for completeness: the formatting for the NationalNumber is '00.00.00-000.00' , the formatting for BIC is AAAA BB CC

I tried annotating my properties with a DisplayFormat Attribute, but that only seems to work with DateTimes, numeric values, etc,..

[DisplayFormat(DataFormatString = "{0:##.##.##-###.##}")]

Then I looked at creating a custom DisplayFormat attribute, but that also works with patterns that apply to DateTimes, numeric values, etc.. You still need to suppy a DataFormatString value in the constructor of your custom attribute. But the filter does not seem to work with strings!

For the moment I ended up doing the markup clientSide(with a mask plugin) but that's not really what I want!

To summarize: I want to use @Html.DisplayFor(x=>x.BicNumber) and have it rendered with a custom formatting, preferrably annotaded on my viewmodel, with BicNumber being a string.

Thanks in advance

7
  • possible duplicate of Why is DisplayFormat DataFormatString not working? Commented Oct 7, 2014 at 16:25
  • have you checked the MSDN documentation on this as well ? msdn.microsoft.com/en-us/library/… Commented Oct 7, 2014 at 16:27
  • Have you tried creating a property in your model to return those fields in their proper formats? Commented Oct 7, 2014 at 16:28
  • @DJKRAZE It is not a duplicate. DisplayFormat never worked for strings in the first place. Commented Oct 7, 2014 at 16:29
  • it's marked as possible duplicate not explicit duplicate.. also I am not sure why the op can't use string.format function along with string.Format("{0:##.##.##-###.##}", what ever value needs to be formatted) Commented Oct 7, 2014 at 16:32

1 Answer 1

2

If your NationalNumber and BIC were separate data types, you could create display templates, store them under ~/Views/Shared/DisplayTemplates/NationalNumber.cshtml and ~/Views/Shared/DisplayTemplates/BIC.cshtml, and it would then automatically work as you want.

If you want to keep these properties as strings, the automatic approach won't work for you because a display template created for string.cshtml would affect all strings in the project.

So create a display template named ~/Views/Shared/DisplayTemplates/NationalNumber.cshtml, where you would manually output parts of the number:

@model string
@String.Format("{0}.{1}.{2}-{3}.{4}", Model.Substring(0, 2), Model.Substring(2, 2), Model.Substring(4, 2), Model.Substring(6, 3), Model.Substring(9, 2))

(note the capitalized String, it is important) and specify it explicitly:

@Html.DisplayFor(x=>x.NationalNumber, "NationalNumber")
Sign up to request clarification or add additional context in comments.

2 Comments

I would add the the only option available in Format Strings for String data type is the alignment, that's why DataFormatString does not work msdn.microsoft.com/en-us/library/txafckwd(v=vs.110).aspx
This is actually a good approach I never thought about but I would like to abstract this logic in our custom framework so that we can re-use the formatting across all of our projects. With your approach we would have to pack this into a nuget or something to get this to work in every project. I was thinking more like a Formatter attribute for my model.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.