Calling controller method with a get type not working in ASP.NET MVC

Advertisements

I have a java script function that calling a controller method using Ajax, this call should get me the user profile page, I have a controller for this purpose. When I run the program and fire the java script function it’s trigger the controller and every thing is good but when the debugging has ended no changes happens and the view doesn’t present in the screen.

I tracked the call and every thing is working fine, passing parameter and reaching the method in the controller.

Note: the view which has the JS call related to a different controller.

View contain java script call:

<td  onclick="ViewProfile('@item.CustomerId')"></td>

Java script file

function ViewProfile(id) {
    console.log("sucsses");
    $.ajax({
        type:"GET",
        url: "/ViewUserProfile/Index",
        data: { "userId": id }
    });
};

Controller: ViewUserProfileController

public ActionResult Index(string userId)
{
    var ProfileInformation = new UserProfileVM
    {
      //some logic here
    };

    return View(ProfileInformation);
}

>Solution :

You are fetching ViewUserProfile in $.ajax but you are not using .done(function() { }) to process the result you receive from that call.

In your case I would simply suggest to use window.location.href in ViewUserProfile() as below.

function ViewProfile(id) {
    window.location.href = "/ViewUserProfile/Index?userId=" + id;
}

Leave a ReplyCancel reply