Reputation: 1309
I am working on webapi here I am getting null data after an ajax call. How could I get the data?
Here my ajax call:
<script type="text/javascript">
$(document).ready(function () {
var editor = CKEDITOR.editor.replace('editor1');
$('#btndiv').mouseleave(function (event) {
$('#btndiv1').hide("slow");
alert(1);
var data1 = editor.getData();
$('#btndiv').append(data1);
// send your ajax request with value
var dataToPost = JSON.stringify(data1);
alert('hi');
alert(data1 + "got data");
$.ajax({
type: "Post",
url: "/api/UpdateCkeditor",
contentType: "application/json; charset=utf-8",
data: dataToPost,
dataType: "json",
statusCode: {
200: function (data1) {
alert("Updated successfully");
}
}
//success: function (value) {
// // do what you want on success.
// alert("Updated successfully");
//}
});
});
});
</script>
and here my controllers:
public void create(ckeditormodels data1)
{
webapiEntities db = new webapiEntities();
var empObj = db.ckeditorDatas.First(c => c.id == 1);
empObj.value = data1.value;
db.SaveChanges();
}
here my models:
public class ckeditormodels
{
public int id { get; set; }
public string value { get; set; }
public string dataToPost { get; set; }
public string data1 { get; set; }
}
Here I am getting my data null. How to get data form ajax call to my controllers? Any help appreciated - thanks.
Upvotes: 0
Views: 712
Reputation: 24125
The getData()
method of CKEditor is returning raw data from editor (the same that editor would post by itself). You need to wrap that string to better fit your model:
<script type="text/javascript">
$(document).ready(function () {
var editor = CKEDITOR.editor.replace('editor1');
$('#btndiv').mouseleave(function (event) {
...
var rawEditorValue = editor.getData();
...
//If you want more properties here just extend this object
var dataToPost = JSON.stringify({ value: rawEditorValue });
...
});
});
</script>
In the result the rawEditorValue
data from editor will be bind to value
property of your model. I also assume here that the request is hitting your action (the URL and method doesn't seem to fit but yoru are saying that the action is hit and only the data is null so I trust you).
Upvotes: 1