Adding a onclick dynamically using an Html Helper in MVC

自作多情 提交于 2019-12-08 11:18:47

问题


I need the ability to dynamically set an onclick using an HTML Helper. the below is what I'm trying to do, but i'm getting an obvious syntax error

<%=Html.CheckBox("checkboxname", item.Id = 3, New With {.onclick = "ajaxThis(this, <%= Html.Encode(item.ID) %>, '<%= Html.Encode(item.NUMBER) %>');"})%>

回答1:


The first step would be to remove the <%= %> from <%= Html.Encode(item.ID) %> and just call Html.Encode(item.ID) directly. Do the same with the item.NUMBER encoding.

Something like:

"ajaxThis(this, " + Html.Encode(item.ID) + ", '" + Html.Encode(item.NUMBER) + "');"



回答2:


You're entering a string, so just concat the string instead:

<%= Html.CheckBox("checkboxname", item.Id = 3, New With {.onclick = String.Concat("ajaxThis(this, ", Html.Encode(item.ID), ", '", Html.Encode(item.NUMBER), "');")})%>

However, it would probably be easier to just add a css class and hook up an event handler using jQuery.



来源:https://stackoverflow.com/questions/899669/adding-a-onclick-dynamically-using-an-html-helper-in-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!