在 Ajax ActionLink MVC 调用中将输入值作为参数传递

Pass input value as parameter in Ajax ActionLink MVC call

我一整天都在四处寻找我的问题的答案,但就是找不到。 有人可以建议一种简单的方法,我可以在 Ajax 调用中将输入字段的值作为参数获取吗? 如果可能的话,我宁愿它保留为 @Ajax.ActionLink 调用。 谢谢

<div class="form-group ui-widget">
    <input class="form-control" id = "Reference" type = "text" />
</div>
<div>
    @Ajax.ActionLink("Select","SelectedReference","Home",
                     new { reference = ?????????? },
                     new AjaxOptions
                     {
                        HttpMethod = "POST",
                        UpdateTargetId = "Results",
                        InsertionMode = InsertionMode.Replace,
                        OnBegin = "OnBeforeReference",
                        OnFailure = "OnAjaxError",
                        OnSuccess = "hideReference"
                    },
                    new
                    {
                       id = "referenceSelectButton",
                       @class = "btn btn-success"
                    })
</div>

Can I get the value of the input field as a parameter in my Ajax call?

不,无法使用 @Ajax.ActionLink 来满足您的要求。

或者,您可以使用 jQuery ajax 来执行此操作。

只需使用一个简单的按钮:

<button class="btn btn-success" id="referenceSelectButton">Select</button>

然后单击按钮使用 jQuery ajax 请求完成所有工作。

@section Scripts{
<script type="text/javascript">
$('#referenceSelectButton').click(function () {
    var referenceVal = $('#Reference').val();
    $.ajax('@Url.Action("SelectedReference","Home",new { reference="-1"})'.replace('-1', referenceVal), {
        method: 'post',
        beforeSend:OnBeforeReference,
        success: function (response) {
            $('#Results').html(response);
            hideReference();
        },
        error: OnAjaxError,
    });
});
</script>    
}

我已将 jquery 脚本代码放在 @section 中,以便它位于 jQuery 脚本之后。希望这能解决您的问题。