0

I am trying to create a the following tag using Razor but am having problems with html escaping.

<input type="button" value="Edit" onclick="EditOnClick('Name','Description')"/>

I have tried the following solutions

string onClick = string.Format("OnEdit(\'{0}\',\'{1}\')", item.Name, item.Description);
<input type="button" value="Edit" onclick="@Html.Raw(onClick)"/>
<input type="button" value="Edit" onclick="@(HttpUtility.HtmlDecode(onClick))"/>
<input type="button" value="Edit" onclick="@(new HtmlString(Model.Content))"/>
1
  • What code does onclick="@Html.Raw(onClick)" actually generate on the client side? You mentioned onclick="EditOnClick('Name','Description')" but I can't see EditOnClick inside string.Format text. Commented Aug 8, 2013 at 22:27

1 Answer 1

1

Why not just do the following:

 <input type="button" value="Edit" onclick="EditOnClick('@item.Name', '@item.Description')"/>

or if they might have html in them:

 <input type="button" value="Edit" onclick="EditOnClick('@Html.Raw(item.Name)', '@Html.Raw(item.Description)')"/>
Sign up to request clarification or add additional context in comments.

2 Comments

I feel like an idiot. The first one works fine, however, Razor flagged it as unterminated string constant with a red line so I thought it was an error. Thanks for your help.
No problem, glad I was able to help :)

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.