无法使用ajax发布的值更新部分cshtml



简单地说,我在一个较大的CSHTML中使用@RenderPage呈现了部分CSHTML中的两个元素。一个是下拉列表,一个是href。(请参阅下面的部分CSHTML。)

当用户从下拉列表中选择不同的名称时,将触发jquery .change()方法,该方法向页面发布(.ajax())一个数值,例如:

@{
    var ajax = Request["picked"];
}
<div id="pt_picker">
    @Html.DropDownList("PTPICKER", ...)
</div>
<div id="switcherlink">
    @{
        @Html.ActionLink("Go", "StartFlow", "Person", new { pid = @ajax, redirectUrl = "InSession" }, null)
    }
</div>
<script type="text/javascript">
    $(document).ready(function () {
        $('#PTPICKER').change(function () {
            $.ajax(
                {
                    url: window.location.pathname,
                    type: 'POST',
                    data: { picked: $("#PTPICKER").val() },
                    success: function (result) { alert("ok"); },
                    error: function (result) { alert("not ok"); }
                }
            );
        });
    });
</script>

使用调试器,数值会被发布,我可以对其进行评估,调试器似乎会用新值再次点击Html.ActionLink行。但是结果页面仍然显示旧值为"pid"的href。

有什么想法吗?提前感谢!

但是结果页面仍然显示旧值为"pid"的href。

这是因为你在成功函数中所做的只是一个提醒。您从未将原始值替换为来自服务器的值。根据您的问题,不清楚控制器操作返回的部分是什么,但您应该使用result变量来替换DOM:中的相应部分

success: function (result) { 
    // You need to inject the result coming from your controller action 
    // (and having the updated values) back into the DOM
    $('#some_id').html(result);
},

最新更新