Consider we have a textarea with ID and Text
- < div >
- @Html.HiddenFor(model = > model.MyNoteViewModels.ICTMyNoteId, new {@id = "ID"})
- @Html.TextAreaFor(model = > model.NoteViewModels.NoteItemText, new {@id = "Text", @onkeyup = "autosave()"})
- < /div>
-
- <script>
- var autosave_timer = null;
- function save() {
- $.ajax({
- type: "POST",
- url: '@Url.Action("SaveNotes", "Note")',
- data: { nID: $('#ID').val() || 0, nText: $('#Text').val() },
- success: function (data) {
- console.log("Notes saved");
- }
- })
- }
- function autosave() {
- if (autosave_timer)
- clearTimeout(autosave_timer);
- autosave_timer = setTimeout(save, 5000);
- }
-
- </script >
We will be using the keyup function to save the Text using the ajaxcall.
- public ActionResult SaveNotes(int nID, string nText)
- {
- MyNoteViewModel model = new MyNoteViewModel();
- model.Id = nID;
- model.Text = nText;
- bool results = false;
- results = _myNMgr.SaveNoteText(model);
- return View(model);
- }
Controller will be having code to save the Text to database.