ASP.Net MVC 3.0 Ajax.ActionLink dynamic object route values using javascript

后端 未结 3 956
执笔经年
执笔经年 2021-02-14 08:11

0 Project

In my view I have a hidden filed which has a UserID. This user id is generated upon an action (so this will not be know prior)

Once this

3条回答
  •  天涯浪人
    2021-02-14 08:58

    When generating the action link on the server you could put some special placeholder for the UserID route value:

    @Ajax.ActionLink(
        "Edit", 
        "EditUser", 
        "User",    
        new { 
            UserID = "__userid__" 
        },
        new AjaxOptions {
            OnSuccess = "ShowEditUserForm",
            UpdateTargetId = "EditUserDetails",
            InsertionMode = InsertionMode.Replace,
            HttpMethod = "Get"
        }, 
        new { 
            @class = "button", 
            id = "EditUserButton" 
        }
    ) 
    

    and then when you assign a value to the hidden field in javascript you could update the action link href as well:

    $('#EditUserButton').attr('href', function() {
        return this.href.replace('__userid__', $('#NewUserID').val());
    });
    

提交回复
热议问题