pass actionlink parameters to jquery function

Asked

Viewed 663 times

0

How would I pass parameters from an Actionlink of a grid to a jquery function?

This is the Actionlink:

 gridPortfolio.Column( format: @<text> @Html.ActionLink("Delete", "DeleteData", new {id = item.CD_PORTFOLIO, par = "PO"}, new { @onClick =  "deleteData()", @class="ActionImgD" })</text>)

and that’s the function:

function deleteData(params) {//conteudo}
  • You will want to continue making the request after or just javascript?

1 answer

1


You can concatenate the parameters when creating the ActionLink.

@Html.ActionLink("Delete", "DeleteData", 
                 new {id = item.CD_PORTFOLIO, par = "PO"}, 
                 new { onclick = "return deleteData('" + item.CD_PORTFOLIO + "', 'PO');", @class="ActionImgD" })


Another option is to use attributes data-*.

@Html.ActionLink("Delete", "DeleteData", 
                 new {id = item.CD_PORTFOLIO, par = "PO"}, 
                 new { onclick = "return deleteData();", 
                       @class="ActionImgD",
                       data_cd = item.CD_PORTFOLIO  /* isso aqui */
                     })

That will generate an attribute data-cd="seuvalor" on the tag. To recover:

function deleteData() {
    var cd = $(this).data('cd');
    //...
}
  • It went more or less right here, the first parameter it is passing it +item.CD_PORTFOLIO+ as a string and not getting the value of my grid already the second one is passing right. I am using it as follows gridPortfolio.Column( format: @<text><input type="image" img src="~/Content/Image/excluir.png" onclick="return deleteData('+item.CD_PORTFOLIO + ', 'PO')" /></text>)

  • You forgot to close the double quotes " before and after the +. Correct would be to put something like: (' " +item.CD_PORTFOLIO + " ',

  • put that way, but now gave Unteminated String Constant Expected ')'. I have trouble putting these quotes. hahahaha.

  • this way now: onclick="return deleteData('" +item.CD_PORTFOLIO + "', 'PO')" /></text>)

  • With this form gave error? Try with the date-*

Browser other questions tagged

You are not signed in. Login or sign up in order to post.