0

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.

3
  • is savehit button is inside a form? Commented Oct 4, 2017 at 6:18
  • Nothing form element on my save button. @jeric Commented Oct 4, 2017 at 6:40
  • Thanks for the answers below.. but unfortunately doesn't solved the issue. Don't know why... You can try to simulate the problem guy's do it from the start with ActionLink. Commented Oct 4, 2017 at 8:11

3 Answers 3

1

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");
            }
        }
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

Also iv'e tried this url: "Home/SaveUpdate", but no luck.
as my investigation the url added the old one like http://localhost:6487/Home/ViewHit/Home/SaveUpdate. it's wierd, don't know why.
You can try to use UrlHelper. try my updated answer. just be sure that 'SaveUpdate' is in Home controller
1
  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)

1 Comment

Thanks for your idea @Unni
1

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");
                    }
                },

            });

        });
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.