سؤال

I am using ActionLink and an ajax call to put a partial view in a parent page. The problem is my controller and the partial view are called twice and on the second call the parameters passed into the action are null.

I’ve tried two versions of the ActionLink. One calls the controller only once but the parameters are null. The second makes two calls and the second time around they’re null

How can I prevent the second call which sends null parameters or if there has to be a second call, how can I sustain the values?

Here’s the link that makes two calls:

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

Here’s the link that makes only one call but the parameters are null

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

Here’s the script:

    $("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);

            }


        });

    });
هل كانت مفيدة؟

المحلول

Your first actionlink will call the javascript you have written while the second one will not (you have not set the class on the second one).

Your first actionlink will call the JavaScript and perform a second call from the link its self. hence the two calls. I am sure one call has the correct parameters and the second is null

You need to match up the parameters your are passing with the action:

var parms = { studentNumber : linkID };

assuming you action has a parameter of studentnumber

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top