Domanda

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 "

È stato utile?

Soluzione

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))
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top