【问题标题】:Why is my parameters not being sent to the controller with this ajaxlink and call?为什么我的参数没有通过这个 ajaxlink 发送到控制器并调用?
【发布时间】:2014-06-16 10:02:24
【问题描述】:

我正在使用 ActionLink 和 ajax 调用将部分视图放在父页面中。问题是我的控制器和局部视图被调用了两次,在第二次调用时传递给动作的参数为空。

我尝试了两个版本的 ActionLink。一个人只调用一次控制器,但参数为空。第二次打了两个电话,第二次他们是空的

如何防止发送空参数的第二次调用,或者如果必须进行第二次调用,我该如何维持这些值?

这是进行两次调用的链接:

     @Ajax.ActionLink(stdFName, "Action", "Controller", new { studentNumber = stdNum }, new    AjaxOptions { UpdateTargetId = "theTimes", InsertionMode = InsertionMode.Replace, HttpMethod = "GET" }, new { @class = "studentName", id = "linkNo" + appendId.ToString() }); 

这是只调用一次但参数为空的链接

   @Ajax.ActionLink(stdFName, "Action", "Controller", new AjaxOptions { UpdateTargetId = "theTimes", InsertionMode = InsertionMode.Replace, HttpMethod = "GET" }); 

这是脚本:

    $("a.studentName").on("click", function () {
        var linkID = this.id;
        var theProp = $(this).attr("href");
        alert(theProp + linkID);

        var equalPosition = theProp.indexOf("=");
        var pram = theProp.substring(equalPosition + 1);
        alert(pram);

        var parms = { data: linkID };

        $.ajax({
            type: "GET",
            url: "/Controller/Action",
            data: parms,
            dataType: "html",
            success: function (data) {
                $("theTimes").html(data);

            }


        });

    });

【问题讨论】:

    标签: javascript jquery asp.net asp.net-mvc-3


    【解决方案1】:

    您的第一个 actionlink 将调用您编写的 javascript,而第二个不会(您尚未在第二个上设置类)。

    您的第一个 actionlink 将调用 JavaScript 并从链接本身执行第二个调用。因此这两个电话。我确定一个调用有正确的参数,第二个是 null

    您需要将您传递的参数与操作相匹配:

    var parms = { studentNumber : linkID };

    假设您的操作有一个参数 studentnumber

    public action Action(string studentNumber){// do stuff and return partial view}

    【讨论】:

      猜你喜欢
      • 2023-04-08
      • 2015-08-06
      • 1970-01-01
      • 1970-01-01
      • 2018-12-10
      • 2017-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多