0
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 "

4
  • String.Format is not raping the text like that, Html.Raw is. Commented Jul 24, 2013 at 10:40
  • The first attempt doesn't use Html.Raw at all, yet it is raping my text. Commented Jul 24, 2013 at 10:41
  • Okay, jumping on Html.Raw was naive of me, but I still guarantee it's not String.Format per se. Commented Jul 24, 2013 at 11:23
  • I agree, I also don't think String.Format is the cause. Perhaps I should have pointed out that the call to string.format is wrapped inside <text> tags to force the output? Commented Jul 24, 2013 at 12:14

1 Answer 1

5

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))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.