Is there a way to implement a textbox that allows users to input data and save it to a database using jQuery AJAX in an Asp.net MVC

How can I implement a textbox that saves user input to a database using jQuery Ajax in an Asp.net MVC4 application? For example, if I type "my new task" in the textbox and click on a button, it should save this input in the database with the project ID and the UserID of the logged-in user. How can I achieve this in MVC4? Here is a snippet of my code:


    public actionresult Insert(task, projectId, UserId)
    {
        //linq to sql goes here...
    }
    

Here is the Ajax code:


    source: function(request, response) {

                     $.ajax({
                     url: pagePath + "/insert",
                         data: "{ 'id': '" + request.term + "'}",
                         dataType: "json",
                         type: "POST",
                         contentType: "application/json; charset=utf-8",
                         dataFilter: function(data) { return data; },
                         success: function(data) {

                             response($.map(data.d, function(item) {
                                 {
                                     value = item.Name 
                                     return value;
                                 }
                             }))
                         },
                         error: function(XMLHttpRequest, callStatus, errorThrown) {
                             alert(callStatus);
                         }
                     });
                 },
    

Could someone please review and offer guidance on whether this approach is correct?

Answer №1

source: function(request, response) {

                 $.ajax({
                 url: "controllertitle/actionmethodtype",
                     data: "{task:value,projectId:value,UserId:value }",
                     dataType: "json",
                     type: "POST",
                     success: function(info) {

                         response($.map(info.d, function(element) {
                             {
                                 val = element.Title 
                                 return val;
                             }
                         }))
                     },
                     error: function(XMLHttpRequest, callResponse, errorThrownInfo) {
                         alert(callResponse);
                     }
                 });
             },

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The TypeScript error message states that a value of 'undefined' cannot be assigned to a type that expects either a boolean, Connection

I've been grappling with this code snippet for a while now. It was originally written in JavaScript a few months back, but recently I decided to delve into TypeScript. However, I'm struggling to understand how data types are properly defined in T ...

Displaying content using asynchronous JavaScript and XML (AJAX

I'm currently working on implementing an infinite scroll feature, where more content is loaded as the user reaches the bottom of the page. I am making an ajax call and attempting to render a new view on the backend before sending it back to the fronte ...

What could be causing ajax_object.ajax_url to continuously show as undefined?

Just getting started with WordPress here. I've set up an on change event in jQuery, loaded my scripts using enqueuing and localized them as well. An issue I'm facing is that when I try to output ajax_object.ajax_url to the console during the on ...

Resource temporarily unavailable in Laravel, unable to unset currently

While working with Laravel, I encountered an issue when trying to delete a file using unset via ajax. The error message thrown by Laravel was: unlink(...) Resource temporarily unavailable After further investigation, I found that if I waited for about ...

Jquery issue: Lightbox unexpectedly closing by itself :(

Help needed: My light box is closing automatically within seconds. $(document).ready(function(){ $('.lightbox').click(function(){ $('.backdrop, .box').animate({'opacity':'.50'}, 300, 'linear'); ...

Having trouble with textures when using THREE.TextureLoader instead of the outdated THREE.ImageUtils.loadTexture?

Recently, I utilized a function to apply texture on a Cylinder shape. function createElementMaterial() { THREE.ImageUtils.crossOrigin = ''; var t = THREE.ImageUtils.loadTexture( IMG_MACHINE ); t.wrapS = THREE.RepeatWrapping; t.wr ...

Problem with finding Rails controller file in .coffee extension

I recently started learning Rails and came across a file called welcome.js.coffee in the assets javascripts directory. # Place all the behaviors and hooks related to the matching controller here. # All this logic will automatically be available in applica ...

Trigger an event upon completion of the angular $route.reload() function

I am encountering an issue where I need to refresh the route using the following code: $route.reload(); However, I also need to trigger a function just before the template is rendered again. For instance: $("#someTab").click(); Your help would be grea ...

Setting up web routing in IIS 10

I am seeking assistance on resolving a website issue. When I visit www.mysite.com, the site loads properly. However, when I try to browse to mysite.com, it redirects me to a different page that just says "PageOk". This redirection was happening with our pr ...

Exploring the contents of an array in ReactJS

const rowData = this.state.market.map((market) => { console.log("details", market["info"]) { return { marketInfo: ( <div> {market && !!market["info"] ? ( <div> ...

What is the best way to change a variable in an AngularJS view?

My Request In my application, I have implemented 3 views, each with its own controller. The first view is the home screen, and from there the user can navigate to view 2 by clicking on a div element. On view 2, the user can then move to view 3 by clicking ...

Embed Javascript Code Within Text Field

Is there a way to incorporate this JavaScript into the "price" text value? Below is the code snippet: <script> function myFunction() { var x = document.getElementById('car-select')[document.getElementById('car-selec ...

JavaScript - Error encountered when accessing information from elements in the DOM

Just started learning javascript and encountering a problem that I can't seem to solve. I'm attempting to assign the className of a <div> element to a variable, but I keep receiving this error message: scripts.js:30 Uncaught TypeError: Can ...

Exploring Kendo Pages with ID Navigation and Displaying Information

In my MVC project, I have two views. From View1, I am retrieving an ID and passing it to View2. In View2, I already have a KendoGrid set up with a controller that fetches data and displays it in the grid. My question is how can I access the data from the ...

JavaScript JCrop feature that allows users to resize images without cropping

I'm currently attempting to utilize JCrop for image cropping, but I'm running into frustratingly incorrect results without understanding why. The process involves an image uploader where selecting an image triggers a JavaScript function that upda ...

What is the best way to create a tooltip that appears when a user hovers over text within an <ins> tag?

Alright, I've defined a custom <ins> tag with the following CSS: ins{ color:blue; font-weight:500; text-decoration:none; } ins:hover{ cursor: pointer; cursor: hand; text-decoration:underline; } I'd like to add a ...

Updating the Navbar in React Routing does not happen automatically, but it does refresh when the page is refreshed

Currently, I am using vanilla React with React-Router for my project. One of the issues I am facing is detecting whether a user on the page has a token, indicating their logged-in status. While setting up routes for Login/Register/Logout functionalities, ...

Having trouble with jQuery AJAX - page continually refreshes upon submitting and data isn't being transmitted

I've been working on creating a dynamic AJAX function using PHP and MySQL to update database records without the need for page refresh or navigation. However, I haven't had much success with it yet. Here's the code I have on the page with t ...

How to Use jQuery to Detect Audio Position in HTML

Is it possible to use the HTML5 Audio tag with an eventListener or action using Dom/jQuery to trigger an event during playback or at the end? This answer mentions currentTime but lacks examples. Pseudo code provided below. <!DOCTYPE html> <html& ...

What is the best way to attach an event listener to detect the coordinates where a click occurs inside a div element?

Imagine a situation where you have a div container measuring 200px by 500px. The goal is to implement an event listener that can identify the x and y coordinates within this div when it is clicked. What would be the approach to achieve this in React? ...