简体   繁体   中英

How to use jquery function result as parameter in html.actionlink

I have simple @html.actionlink("action", "controller", new {id = somevalue})

and jquery function

<script>
    function getValue() {
        var p = $("#EditId").val();
        return p;
    }
</script>

How to use return value of this function instead of " somevalue " in actionlink?

Try this:

@html.actionlink("action", "controller", new {id = somevalue,@class="link"})

<script>
function getValue() {
    var p = $("#EditId").val();

    return p;
}
$(document).ready(function(){

 $(".link").attr('id')=getValue();
  });
   </script>

Or:

 $(".link").href.replace("somevalue", getValue());

What you are trying to do is impossible as you describe it because the @Html.ActionLink is a server side code and renders a tag when JS involved the @Html.ActionLink finished its job and gone.

Also as you are trying change the id of element this might be also impossible for later access (not sure).

As a work around you can ad the link with JS function with a place-holder. Try below and tell me is it is working

instead of @Html.ActionLink use the below;

<div id="linkPlaceHolder"></div>

on load use the following

$(function(){
 var actionUrl = @Url.Action("action", controller); // you get the url of action

 $("#linkPlaceHolder").replaceWith("<a href='"+actionUrl+"' id='" + getValue() + "'>Text</a>")
});

this should work and if not or not exactly what you want we are here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM