Adding dynamic onclick with Html Helper in MVC

I need the ability to dynamically set onclick using an HTML helper. below is what I am trying to do, but I am getting an obvious syntax error.

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

      

0


a source to share


2 answers


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

Sort of:



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

      

+1


a source


You are entering a string, so just run the following line:

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

      



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

+1


a source







All Articles