0

I have a problem with Razor and String.Format. I have a parameter as follow:

@{
string aktenHeader = Html.Raw(Model.Node.Aktenzeichen + string.Format("{0}" + Model.Node.Aktenkurzbezeichung, (Model.Node.Aktenzeichen != null && Model.Node.Aktenkurzbezeichung != null ? " ./. " : ""))).ToString();    
}

and I got the exception : Input string was not in a correct format.

Please help me friends. Thanks

EDIT

Model.Node.Aktenzeichen = "Akte"

Model.Node.Aktenkurzbezeichung = "Test ${}"

And I expected aktenHeader = "Schnulli ./. Test ${}"

Ragnarsson
  • 1,715
  • 7
  • 41
  • 74
  • What is the value of `Model.Node.Aktenkurzbezeichung`? – Andrei Jun 26 '13 at 13:34
  • What's the value of `Model.Node.Aktenkurzbezeichung`, and what are you trying to achieve? The code is relatively convoluted, and we've no idea what the data looks like or what you want the result to be. – Jon Skeet Jun 26 '13 at 13:34
  • @JonSkeet, Andrei: I updated my post. Thanks for quick responses. – Ragnarsson Jun 26 '13 at 13:40

2 Answers2

6

You have to escape your curly braces in Model.Node.Aktenkurzbezeichnung.

Model.Node.Aktenkurzbezeichung = "Test ${{}}"

This question is already be answered i.e. here: How to escape braces (curly brackets) in a format string in .NET

If you cannot change the/replace the braces as needed, then I think the easiest way of doing this is to not use string.Format():

@{
   string aktenHeader = Html.Raw(Model.Node.Aktenzeichen + (Model.Node.Aktenzeichen != null && Model.Node.Aktenkurzbezeichung != null ? " ./. " : "") + Model.Node.Aktenkurzbezeichung).ToString();    
}
Community
  • 1
  • 1
Kevin Brechbühl
  • 4,717
  • 3
  • 24
  • 47
  • It might not be possible in OP's case - what if the string comes from DB and is used in several other places as is? – Andrei Jun 26 '13 at 13:45
  • I would suggest that escaping is the wrong approach here - it simply shouldn't be part of the format string, as far as I can see. – Jon Skeet Jun 26 '13 at 13:48
  • I would also not replace it within the format, I would replace it in the ViewModel if it's possible. Otherwise I would work without the string.Format(). I've updated my post. – Kevin Brechbühl Jun 26 '13 at 13:51
  • @KevinBrechbühl: hey thanks, I don't use string.Format() anymore, and I use your second approach, the problem is solved :) – Ragnarsson Jun 26 '13 at 13:54
2

It sounds like you shouldn't really be including Model.Node.Aktenkurzbezeichung in your query string at all. You're just trying to concatenate values in the end, so I suspect you can use:

@{
    string x = Model.Node.Aktenzeichen != null &&
                   Model.Node.Aktenkurzbezeichung != null ? " ./. " : "";
    string aktenHeader = Html.Raw(Model.Node.Aktenzeichen + x +
                                  Model.Node.Aktenkurzbezeichung).ToString();    
}

I've extracted the long expression out as a separate local variable just for clarity.

If you want to use string.Format, you can still do so:

string aktenHeader = Html.Raw(string.Format("{0}{1}{2}",
                              Model.Node.Aktenzeichen, x,
                              Model.Node.Aktenkurzbezeichung)).ToString();

Fundamentally, it's important to be clear about which parts of your data are meant to be used as a string format, and which parts are meant to be used as values. You were using Aktenkurzbezeichung as part of the format when it looks like you wanted it to just be treated as a value.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194