Lord Vermillion
Lord Vermillion

Reputation: 5414

MVC Razor string.format in html.raw

I use this razor code to generate a HTML button that calls a function with a value from the model:

Html.Raw(string.Format("<button type='button' class='btn btn-success btn-xs' onclick='setCoordinatorForService('{0}')'>Åta</button>", item.Name))

The value of item.Name is "abc", the code:

<button type="button" class="btn btn-success btn-xs" onclick="setCoordinatorForService(" abc')'="">Åta</button>

I want this:

<button type="button" class="btn btn-success btn-xs" onclick="setCoordinatorForService("abc")'="">Åta</button>

What am i doing wrong?

Upvotes: 1

Views: 8100

Answers (1)

bafsar
bafsar

Reputation: 1108

You must replace ' to \"

If you use this code, probably it works very well.

Html.Raw(string.Format("<button type=\"button\" class=\"btn btn-success btn-xs\" onclick=\"setCoordinatorForService('{0}')\">Åta</button>", item.Name)))

Besides the line you wanted it has error.

onclick="setCoordinatorForService("abc")'=""

part maybe give an error because of =

Upvotes: 6

Related Questions