Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading