0

I have a function here from a change event on a dropdownlist. When the selection gets changed I want to update a row in my database. Should I use javascript or ajax. I don't want the page to be refreshed. I think it should be ajax, but not sure? If ajax, can anyone point me to a tutorial/video/etc?

Here is where I want to update my db row.

var statusdropdown = document.getElementById("enumstatus");
statusdropdown.addEventListener("change", function(event) {
    // call db and update row
}, false);
3
  • javascript or ajax? ...you realize ajax means asynchronous javascript(and xml)...right? You definitely want ajax. Commented Feb 18, 2015 at 22:09
  • not too familiar with asp.net...you basically want to pass the current value of #enumstatus to the function that updates the db. Commented Feb 18, 2015 at 22:11
  • do you have a function that you've written to update the db yet? Commented Feb 18, 2015 at 22:12

4 Answers 4

2

Looks like you using asp.net mvc.

You can write your ajax calls with pure javascript Ajax docs or the easiest way, using JQuery.

You need to add one action on your controller to receive the ajax data, and then insert/update your db.

See this, this and this.

Sign up to request clarification or add additional context in comments.

Comments

0

Most common scenario would be making an ajax call using HTTP POST/PUT to a controller method, which would then handle the data and update the database directly or pass through to your service/data layer code.

Probably the easiest way to make the call would be using the jQuery.ajax method. Documentation can be found here: http://api.jquery.com/jquery.ajax/

Comments

0

You can try something like this

<script type="text/javascript">
    $(function () {
        $('#btnSubmit').click(function () {
            var name = $('#TextBox1').val();
            var email = $('#TextBox2').val();
            if (name != '' && email != '') {
                $.ajax
                    ({
                        type: 'POST',
                        url: 'Home/UpdateDB',     //if it is plain asp.net then UpdateDB is declared as WebMethod 
                        async: false,
                        data: "{'name':'" + name + "','email':'" + email + "'}",
                        contentType: 'application/json; charset =utf-8',
                        success: function (data) {
                            var obj = data.d;
                            if (obj == 'true') {
                                $('#TextBox1').val('');
                                $('#TextBox2').val('');
                                alert("Data Saved Successfully");
                            }
                        },
                        error: function (result) {
                            alert("Error Occured, Try Again");
                        }
                    });
            }
        })
    });
</script> 

Comments

0
  • fg
  • iuou
  • uouienter link description here
  • uiouidfgsdfgddfgdfgdfgdgdgd##


















Heading


##*

  • uio
  • uiopomkl
  • hjlkdsg
  • dsf
  • dfgdg

Blockquote

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.