C # String.Format - invalid input string

I have an MVC3 HtmlHelper extension, for example:

public static MvcHtmlString ShowInfoBar(this HtmlHelper helper, string message, InfoMessageType messageType) { return MvcHtmlString.Create(String.Format(@"<script type=""text/javascript"">$(document).ready(function () { gtg.core.showInfoBar('{0}', '{1}'}; );</script>", message, messageType.ToString().ToLower())); } 

The value of message is "The product "Product Name" was saved successfully."

messageType is info .

He keeps saying Input string was not in the correct format.

I am stuck??

+7
source share
5 answers

On each curly brace that is not a token, you must double -

 function() {{ 

Etc

Also - consider XSS here - is this message properly escaped for insertion into JavaScript?

+19
source

Reset your square brackets {{}} in the format string

 String.Format(@"<script type=""text/javascript"">$(document).ready(function () {{ gtg.core.showInfoBar('{0}', '{1}'); }});</script>", message, messageType.ToString().ToLower()) 
+10
source

You need to avoid braces:

{{ and }}

 String.Format(@"<script type=""text/javascript"">$(document).ready(function () {{ gtg.core.showInfoBar('{0}', '{1}'}}; );</script>", message, messageType.ToString().ToLower()) 
+5
source

In my case, I used a bracket to format JsonP. JsonP also requires "{". Breaking out of {like this: '{{', my problem was resolved.

+1
source

I tried, as shown below, and its work.

 string.Format(@"ExecuteOrDelayUntilScriptLoaded(function () {{ Your function. 
0
source

All Articles