0

I am working with jQuery. I need to insert data into database in webapi (mvc4) here I have data in <p> now I need to insert that data into database.

Here is my code:

 <script type="text/javascript">
     $(document).ready(function () {
     $('#btndiv1').hide();
     $('#btndiv').hover(function () {
        $('#btndiv1').show();
    });
    });
 </script>
     <script type="text/javascript">
    $(document).ready(function () {
    var editor = CKEDITOR.editor.replace('editor1');
    $('#btndiv').mouseleave(function (event) {
        $('#btndiv1').hide("slow");
            alert(1);
            var value = editor.getData();

            $('#btndiv').append(value);
        // send your ajax request with value

            var dataToPost = JSON.stringify(value);
            alert(dataToPost);
            ale(value);
            $.ajax({
                type: "POST",
                url: "/CkeditrorData/create",
                contentType: "application/json; charset=utf-8",
                data: dataToPost,
                dataType: "json",
                success: function () {
                    // do what you want on success.


                }
            });
        });
    });

</script>

and here is my html:

     <h4>design your own text</h4>
<div id="btndiv" >
    <div id="btndiv1" style="height:auto;width:auto; border:solid 1px;">
    <textarea  rows="10"   id="editor1" "></textarea>
    <p id="text" ></p>
        </div>
</div>

Now I have data in <p> tag above; I need to insert data into the database. How could I do this? Could you please help me?

2 Answers 2

3

You need to make an ajax call to your action, which will make an insert to the database.

Below is a very simple ajax call,

var Contact = {
Lastname: 'Smith',
Firstname: 'John'
};

var dataToPost = JSON.stringify(Contact);

$.ajax({
    type: "POST",
    url: "/contacts/create",
    contentType: "application/json; charset=utf-8",
    data: dataToPost,
    dataType: "json",
    success: function(){
        // do what you want on success.
    }
});

On server create a class with a similar structure for the contact class.

public class Contact
{
public string Lastname { get; set; };
public string Firstname { get; set; };
}

below is the action to which the values will be posted using jquery.

[HttpPost]
public string Create(Contact contact)
{
    // insert this contact to db.
}
2
  • @yaseer mouseleave function not firing means to not moving to controller could u plz help me i have updated my question could u plz check it Oct 29, 2012 at 13:49
  • @yaseer This is very good sample, and it works for me and i can insert data into database, but it does not go into success function and it goes into error function. why ?
    – Persian.
    Oct 24, 2013 at 22:33
1

You must use ajax to send data to server and save to database

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.