Question

String.Format("args.{0} = '{1}'", dynaform.RequiredModelName, dynaform.RequiredModel)

Expected result:

args.myVariable = 'someOtherStuff'

Actual result:

args.myVariable = 'someOtherStuff'

Second attempt:

Html.Raw(String.Format("args.{0} = '{1}'", dynaform.RequiredModelName, dynaform.RequiredModel))

Result:

Object reference not set to an instance of an object.

How can I tell String.Format to output ' as ' and not as '? This is really frustrating and, since now, I had to avoid String.Format when dealing with ' or "

Was it helpful?

Solution

Use MvcHtmlString.Create()

From the specs: Creates an HTML-encoded string using the specified text value.

In the posted code:

MvcHtmlString.Create(String.Format("args.{0} = '{1}'", dynaform.RequiredModelName, dynaform.RequiredModel))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top