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

后端 未结 3 1061
执笔经年
执笔经年 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:32

    The answer provided by Darin is great and helped me, however as the comment suggests if you need to click the link again and pass a different value, how do you do that? This is a requirement if you are updating partial views etc. So here is how I achieved exactly that...

    $(document).ready(function () {
        $('#replyMessageButton').click(function () {
            var url = $("#replyMessageButton").attr("href")
            $("#replyMessageButton").attr("href", TrimToSlash(url) + $("#MessageId").val())
        });
    });
    
    function TrimToSlash(value) {
    
        if (value.indexOf("/") != -1) {
    
            while (value.substr(-1) != '/') {
                value = value.substr(0, value.length - 1);
            }
        }
        return value;
    }
    
            @Ajax.ActionLink("Reply", "ReplyMessage", "MessageBox", new { id = -1 },
                            new AjaxOptions
                                {
                                    UpdateTargetId = "replyMessageContainer",
                                    InsertionMode = InsertionMode.Replace,
                                    OnBegin = "UpdateDisplay('replyMessage')",
                                    OnFailure = "UpdateDisplay('default')"
                                },
                                new { @id = "replyMessageButton" }
                                )
    

    Also implemented is a check for messageId > 0 in the controller, hence why the id is initialized to -1. An "Error" view is returned if this condition is not met.

提交回复
热议问题