2

I have a Html.TextBoxFor which I would like to assign some dynamic javascript to (the onchange event specifically):

@Html.TextBoxFor(model => answer.Value, new { @class = "answerinput", @onchange = "submitAnswer(\"" + Model.QuestionID.ToString() + "\")" });

However, when I examine the resulting HTML, the quotes around the value passed into the javascript function are encoded, which is a problem:

onchange="submitAnswer("3")"

I've tried a few things, like placing the string into an IHtmlString and then using the IHtmlString in the assignment but the results are always the same.

Is there a way to prevent MVC from encoding the value assigned to @onchange?

Thanks!

jasongullickson
  • 1,021
  • 1
  • 14
  • 28

2 Answers2

4

Alternatively, you should change the way you are attaching to the event:

Html.TextBoxFor(x => x.SomeProperty, new { rel = Model.QuestionID, @class = "SomeClass" });

Then in javascript, attach to the event:

$('.SomeClass').each(function()
{
     $(this).change(function()
     {
          var questionId = $(this).attr('rel');
     });
});
Tejs
  • 40,736
  • 10
  • 68
  • 86
-1

Try using single quotes, no escape character required.

@Html.TextBoxFor(model => answer.Value, new { @class = "answerinput", @onchange = "submitAnswer('" + Model.QuestionID.ToString() + "')" });
tim
  • 29
  • 2