<input type="button" value="Delete" onclick="deleteMember(@Model.ID,'@ViewBag.returnUrl')"/>
解决方法
您可以在Html.Raw调用中将其分解:
<input type="button" value="Delete" onclick="@(Html.Raw(String.Format("deleteMember({0},'{1}')",Model.ID,Html.Encode(ViewBag.returnUrl) )))" />
或者首先将其分配给字符串,然后在属性中引用该变量.
@{ String onclick = String.Format("deleteMember({0},Html.Encode(ViewBag.returnUrl)); } <input type="button" value="delete" onclick="@onclick" />
第三种选择是分开关注;保持标记和脚本分开. Neil击败了我,但有点像:
<input type="button" value="delete" data-task="delete" data-id="@Model.ID" data-returnurl="@ViewBag.returnUrl" /> <!-- .... --> <script> var buttons = document.getElementsByTagName('INPUT'); for (var b = 0; b < buttons.length; b++){ if (buttons[b].type != 'button') continue; switch (buttons[b].getAttribute('data-task') || ''){ case 'delete': // read data-id & data-returnurl. Do something with them,and // move on. break; // other actions case 'add': case 'update': default: break; } } </script>
虽然奇怪的是第一个括号与剃刀混淆(即使它在属性中声明).我自己也不会期待这种行为.