asp.net-mvc-2actionlink

asp.net mvc2 actionlink with parameters similar to getelementbyid


My Action link is as follows: <%: Ajax.ActionLink("linktext", "actionName", "controllerName", new {area="areaName",id = "abcd", productURLName = "choc"}, new AjaxOptions { UpdateTargetId = "SFS", InsertionMode = InsertionMode.Replace, HttpMethod = "Post" }, new { style = "color:#00FF00" })%>

I need to get the value of a control in runtime and pass it as parameter in place of "abcd".

In javascript I can do getelementbyId. Is there anything I can do in this case.

To get similar functionality I have also tried jquery ajax, but in that case the controller action is not even getting called, at least the action link works::

function SendInvite() {

              var url = "areaName/controllerName/actionName/" + document.getElementById("BasicUserInfo").value + "?productURLName=choc";
              $.post(url, function (data) {
                                        if (data == '<%= Boolean.TrueString %>') {
                                            $("#result").append("Invite Sent");
                                        } else {
                  $("#result").append("Error, Please try later");
                                        }
              });
          }

Thanks Arnab


Solution

  • You have to write your jQuery code at click event of the link. For example this is your link:

    <%:Html.ActionLink("linkText", "actionName", "controllerName", new {area="areaName",id = "abcd", productURLName = "choc"},  new {@class="myclass", style = "color:#00FF00" })%>
    

    Then you have to write jQuery function that hooks the click event of this link. Here I'm using class selector:

     $(".myclass").live('click', function(){
                      var url = "areaName/controllerName/actionName/" + document.getElementById("BasicUserInfo").value + "?productURLName=choc";
                      $.post(url, function (data) {
                                                if (data == '<%= Boolean.TrueString %>') {
                                                    $("#result").append("Invite Sent");
                                                } else {
                          $("#result").append("Error, Please try later");
                                                }
                      });
    return false;
    
    
        });
    

    You can also get URL in event handler using:

    var url = $(this).attr('href');