Ajax request not populating controller with data in ASP.NET CORE MVC

`Hello everyone, I'm running into a problem with my colleague's assignment and could really use some assistance. The issue pertains to ASP.NET Core MVC. I have an API Controller for editing student groups. This API Controller receives a GroupViewModel as a parameter for updating purposes. While the model received is valid, the variables within it are showing up as null. When examining the request being sent from the client-side, data is present. Your insights and help are greatly appreciated!

View Image 1 View Image 2 View Image 3`

Answer №1

There are a couple of approaches you can take:

Approach 1: Consider removing JSON.stringify and contentType from your AJAX call:

 $.ajax({
     type: 'POST',
     url: '/Enrollment/EditGroup',
     data: updatedGroup,
     success: function (r) {
             console.log("RESULT", r);
              },
    error: function (xhr, status, error) {
            var err = eval("(" + xhr.responseText + ")");
            alert(err.Message);
    }
 });

This would yield the following outcome:

https://i.sstatic.net/Z111a.png

Approach 2: Another option is to include [FromBody] in your controller method signature like so:

[HttpPost]
public IActionResult EditGroup([FromBody]GroupViewModel _data)
{
   //perform necessary actions here
}

This approach would result in:

https://i.sstatic.net/PV1FJ.png

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

An issue arises when trying to import the modal popup

Hey there! I'm currently trying to implement a modal popup in react-bootstrap, but I keep running into an error that says "Props not defined". I followed the instructions from the react-bootstrap documentation, but I can't seem to figure out what ...

PHP ajax needs to solely showcase error messages upon form submission

When I submit my form data to a PHP file, it checks for any errors. However, it also returns success before redirecting through AJAX. My goal is to only display an error message if there is one, and if successful, redirect to another page. AJAX: $("#msf ...

Creating functions within the $scope that do not directly access the $scope object

tag, I am looking to create a $scope function that specifically manipulates the variables it receives. To test this functionality, I have set up a Plunker available at http://plnkr.co/edit/BCo9aH?p=preview. In my setup, there is an ng-repeat loop that lis ...

Caution: It is essential for each child within a list to possess a distinct "key" property - React Native issue alert

Hello everyone, I am currently working on building a list in an app using react-native. I have been following a tutorial video and my code is identical to the instructor's, but I keep encountering an error that says each child in the list needs a key ...

Tips for identifying and swapping values/parameters in a URL during redirect

To provide more clarity on my inquiry, I will outline the details below: There are three links: Link A, Link B, and Link C Link A: {c_val} Link B: {id} Link C: In the database, there is a value adgaf7g6adf6gadfg8a86fgs9f6g The main focus here is when ...

Enhancing functionality in Javascript by employing prototype descriptor for adding methods

Code: Sorter.prototype.init_bubblesort = function(){ console.log(this.rect_array); this.end = this.rect_array.length; this.bubblesort(); } Sorter.prototype.init = function(array,sort_type){ this.rect_array = array; this.init_bubblesort(); } Wh ...

Is there a way to include a HyperLinkField in a GridView that is bound to a directory of files?

I am attempting to create a GridView with a hyperlinkfield column that displays filenames from a server file directory. The code I have been using is not producing the desired result - instead of clickable hyperlinks, it displays the path and filename. I ...

Using Grails to render a PDF file through an AJAX request with pdf.js

I'm currently working on implementing a new feature in our application that involves embedding a PDF viewer. The challenge is to allow users to view the PDF while also accessing other forms on the screen simultaneously. For security reasons, we canno ...

How to extract hrefs within <li> tags from a <ul> element with Cheerio

I'm facing some difficulties with this question, so I need your help. What I want to achieve is to extract the hrefs from the HTML provided below. <ul id="nav-products"> <li><a class="" href="/shop/hats/">yellow good looking ha ...

Accessing website using Facebook credentials

I'm currently in the process of integrating Facebook Login on my website and have a few questions. I've encountered a problem where, upon receiving user permission, I need to create a new account in my database in order to utilize certain functio ...

Sending an AJAX request to a secure HTTPS URL

As my website is utilizing an SSL connection, I am currently examining the port through which the user accesses the URL (http/https): if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['H ...

Bidirectional data binding in Angular 2 for the class attribute

Utilizing twitter bootstrap tabs, I aim to monitor the application of the active class on the li tag as users switch between different tabs. My objective is to control tab activation through custom buttons by modifying the class attribute to activate direc ...

The process of accessing a JSON object within an AJAX onreadystatechange function

Currently, I have a servlet calling an ajax request which sends back a JSON object in response. My task now is to receive this JSON in JSP and display the data in table format. Can anyone provide assistance with this? Here is my current code snippet: The ...

Tutorial on React JS and Python Django: Understanding the concept of an infinite loop in componentDidUpdate

I'm currently going through an online video tutorial that teaches how to create a CRUD full-stack app using React, Django, and SQLite. However, I've encountered an issue with fetching table data causing an infinite loop. It appears that the probl ...

Is it possible to install the lib ldap-client module in node.js?

I'm having trouble installing the lib ldap-client package in Node.js. To try and solve this issue, I consulted the following page: https://github.com/nodejs/node-gyp. In an attempt to fix the problem, I have installed python, node-gyp, and Visual St ...

What is the best method for saving console.log output to a file?

I have a tree structure containing objects: let tree = {id: 1, children: [{id: 2, children: [{id: 3}]}]} My goal is to save all the id values from this tree in a text file, indenting elements with children: 1 2 3 Currently, I am using the following ...

The iframe came to a halt as soon as it was no

I am currently developing a live video website that utilizes third-party tools to play the videos. To simplify things, I have embedded all the components required for live video into a single HTML page. Here is how it looks: <iframe data-v-140cfad2= ...

Leverage the power of personalized SCSS styles in combination with Bootstrap-Vue, Webpack, and

I have been working on an application using VueJS with Bootstrap-Vue and I encountered a challenge while trying to import a SCSS file to customize the Bootstrap variables and add some custom styles. Here are the steps I have taken so far: Installed node ...

No results found by Mongoose find() method

I've set up a route that utilizes a model named Todo as shown below: app.get('/api/todos', function(req, res) { Todo.find({},function(err, todos) { if (err) res.send(err); console.log("number of todos " + tod ...

Submitting content without needing to refresh the page occurs when clicking on a link rather than a button

I am sending parameters from one page to another so that they can be inserted into a table when a link is clicked. I'm interested in knowing if it's possible to use AJAX to call the insert script when text is clicked. I know this can be done with ...