Prevent encoding of javascript assigned to onchange event of Html.TextBoxFor via htmlAttributes
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!
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');
});
});
Try using single quotes, no escape character required.
@Html.TextBoxFor(model => answer.Value, new { @class = "answerinput", @onchange = "submitAnswer('" + Model.QuestionID.ToString() + "')" });
精彩评论