开发者

asp.net mvc2 actionlink with parameters similar to getelementbyid

开发者 https://www.devze.com 2023-02-19 14:42 出处:网络
My Action link is as follows: <%: Ajax.ActionLink(\"linktext\", \"actionName\", \"controllerName\", new {area=\"areaName\",id = \"abcd\", productURLName = \"choc\"}, new AjaxOptions { UpdateTargetI

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


You may get rid of MicrosoftAjax and try like this:

<%= Html.ActionLink(
    "linktext", 
    "actionName", 
    "controllerName", 
    new { area = "areaName", productURLName = "choc" }, 
    new { style = "color:#00FF00", id = "myLink" }
) %>

and then:

$(function() {
    $('#myLink').click(function() {
        var id = $('#BasicUserInfo').val();
        $.post(this.href, { id: id, productURLName: 'choc' }, function(result) {
            $('#SFS').html(result);
        });
        return false;
    });
});


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');
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号