Vijunav Vastivch
Vijunav Vastivch

Reputation: 4191

Ajax Jquery not return the URL after using ActionLink in MVC

I got a scenario:

Here's my ActionResult when ActionLink was Clicked

 public PartialViewResult ViewHit(string id, string recordid,string statusSelect,string statusHitSelect)
{
    //some code here....
}

The ActionLink where to post after the Button was clicked:

public ActionResult SaveUpdate(String statusSelect, string statusHitSelect)
 {

     return PartialView("ViewHitList");
 }

Here's the button:

<input type="button" value="Save" id="savehit"/>

and Here's my Ajax:

 $("#savehit").on('click', function () {

            //alert("Button was click!");
            $.ajax({
                type: "post",
                contentType: "application/json; charset=utf-8",
                //url: "SelectUpdate?statusSelect=" + statusSelect + '&statusHitSelect=' + statusHitSelect,
                url: "SaveUpdate",
                data: "{'statusSelect':'" + statusSelect + "','statusHitSelect':'" + statusHitSelect + "'}",
                //data:null,
                success: function (response) {
                    if (response != null && response.success) {
                        //InformationMessageSuccess(response.responseText);
                        alert("success");
                    } else {
                        // DoSomethingElse()
                        //InformationMessageFailed(response.responseText);
                        alert("not success");
                    }
                },

            });

        });

The problem is, when i hit the save button using debug mode the ActionResult called was the ViewHit instead of SaveUpdate.

I am wondering why is it happen?

Any great idea is highly appreciated.

Upvotes: 0

Views: 68

Answers (3)

Jeric Cruz
Jeric Cruz

Reputation: 1909

You can try to avoid default action of the event by using event.preventDefault()

$("#savehit").on('click', function (event) {
    event.preventDefault();
    //alert("Button was click!");
    $.ajax({
        type: "post",
        contentType: "application/json; charset=utf-8",
        //url: "SelectUpdate?statusSelect=" + statusSelect + '&statusHitSelect=' + statusHitSelect,
        url: "@Url.Action("SaveUpdate", "Home")",
        data: "{'statusSelect':'" + statusSelect + "','statusHitSelect':'" + statusHitSelect + "'}",
        //data:null,
        success: function (response) {
            if (response != null && response.success) {
                //InformationMessageSuccess(response.responseText);
                alert("success");
            } else {
                // DoSomethingElse()
                //InformationMessageFailed(response.responseText);
                alert("not success");
            }
        }
    });
});

Upvotes: 1

sina_Islam
sina_Islam

Reputation: 1163

SaveUpdate is not post type please add attribute [HttpPost] at the top of the controller method like

[HttpPost]
public ActionResult SaveUpdate(String statusSelect, string statusHitSelect)
 {

     return PartialView("ViewHitList");
 }

I have changed the ajax call like bellow and it the SaveUpdate method. Bellow is the ajax call.

  $(document).ready(function () {
        $("#savehit").on('click', function () {
            var statusSelectData="statusSelect";
            var statusHitSelectData="statusHitSelect";
            var postData = {
                statusSelect: statusSelectData,
                statusHitSelect: statusHitSelectData
            }
            //alert("Button was click!");
            $.ajax({
                type: "POST",
                url: "/Home/SaveUpdate",
                data: postData,
                //data:null,
                success: function (response) {
                    if (response != null && response.success) {
                        //InformationMessageSuccess(response.responseText);
                        alert("success");
                    } else {
                        // DoSomethingElse()
                        //InformationMessageFailed(response.responseText);
                        alert("not success");
                    }
                },

            });

        });
    });

Upvotes: 1

Unni Krishnan
Unni Krishnan

Reputation: 94

  1. Add the [HttpPost] attribute to the SaveUpdate action
  2. In the ajax, change the URL to url: "ControllerName/SaveUpdate"
  3. Since you are expecting a boolean result (JSON) back to your page as the ajax response, simply return a jsonResult (true/false)

Upvotes: 1

Related Questions