Pregunta

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 "

¿Fue útil?

Solución

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))
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top