• 0

Sending an array via AJAX to MVC Controller


Question

First, let me start by saying that, though I'm a seasoned developer with 30 years behind the keyboard, I've only been working with web development for about a year, so there are things I don't know.  However, this one really has me confused!

 

All I want to do is send an array of integers to an MVC controller using an AJAX call.  The code below works absolutely perfectly for me every single time I run it in my development environment (Windows 10, VS2017), BUT, when I roll it out to the production server (running IIS 7.5), I get an error 500, internal server error.  To make matters worse, this code WORKED on the production environment 1 week ago, and yesterday it decided "screw you!" and started to fail.

 

Note, when it errors, it jumps into the error part of the Ajax call; it never gets into the controller method, I guess because it doesn't like the int[] parameter anymore...

 

Here is my AJAX call:

	var selectedItems = new Array();
	$("input:checkbox[name=selectPrice]:checked").each(function () {
		selectedItems.push(this.id);
	});

	if (selectedItems.length > 0) {
		$.ajax({
			url: "/ProductPrice/CanApprove",
			type: "post",
			dataType: "json",
			contextType: "application/json",
			data: { selectedIds: selectedItems },
			traditional: true,
			success: function (result) {
				if (result.success) {
					//Do something useful
				} else {
					//It all went wrong
				}
			},
			error: function (_err) {
				//It all went REALLY wrong
			}
		});
	}

 

And here is my controller method:

[Authorize]
[HttpPost]
public JsonResult CanApprove(int[] selectedIds)
{
	bool canApprove = service.CanApprove(selectedIds, out string reason, out int[] approvableIDs);
	return Json(new { success = canApprove, ex = reason, approvableIDList = approvableIDs }, JsonRequestBehavior.AllowGet);
}

 

I've had to change over to just passing a delimited string for now, and converting it back into an array inside my controller method (which is just urgh!), but I'm REALLY struggling to understand why this just stopped working?!  Is there anyone here with more javascript experience than me that can explain what could have caused this?

 

 

6 answers to this question

Recommended Posts

  • 0

Your ajax code is not sending an int[] array but an object with an int[] array:

 

What you expect it to send:

[1,2,3,4]

 

What it actually sends:

{"selectedIds": [1,2,3,4]}

 

Change data property in your ajax call to:

data: selectedItems,

 

That should probably work if my assumptions were correct :p

 

It's been a while since I worked with MVC controllers but as far I remember you don't need to send the controller parameter key in the object data if there is a single parameter in the controller method.

  • 0

Yeah, I tried that, and other variants. Without the name of the parameter, all the controller receives is NULL. :(

 

Anyway... I forgot to post, I actually found a solution to the problem late yesterday afternoon, after much pulling out of hair, and commenced to kicking myself repeatedly once I'd found it!

 

I was on the verge of just giving up completely when I decided that, even though the controller code was simple, I'd stick it inside a try/catch (which I should have done from the start anyway, because y'know, DUH!).  And when I did that, I was finally able to get a sensible error message in the AJAX call.  It seems that for some reason, my call was having trouble opening a database connection as the error complained that the connection as in use!

 

Ah ha! says I. I've seen that before and immediately went to look at the webconfig file, followed immediately by going over to our DBA and punching him in the side of the head.  It seems that when he last updated the webconfig when they changed the password a few days ago, he REMOVED the command "MultipleActiveResultSets=True" from the connection string.

 

IDIOT! IDIOT! IDIOT!

 

I'm amazed the app was functioning at ALL with that missing!

 

Moral of this story folks... IT'S THE DBA'S FAULT! :p

 

 

  • Haha 1
  • 0
  On 28/09/2017 at 17:17, Sledge said:

I think moral of the story is poor error handling.

 

Expand  

 

  On 23/09/2017 at 10:38, FloatingFatMan said:

 

Moral of this story folks... IT'S THE DBA'S FAULT! :p

 

Expand  

Seems the DBA is here and throwing the blame back at you :rofl:

 

But I have to agree with the DBA on this one, too many times I programmed a try catch without outputting the error in the catch resulting in missing the error and wondering what i did wrong :pinch:

  • 0
  On 28/09/2017 at 19:25, Seahorsepip said:

 

Seems the DBA is here and throwing the blame back at you :rofl:

 

But I have to agree with the DBA on this one, too many times I programmed a try catch without outputting the error in the catch resulting in missing the error and wondering what i did wrong :pinch:

Expand  

Hehe - nah just too many years covering my ass

  • 0
  On 28/09/2017 at 17:17, Sledge said:

I think moral of the story is poor error handling.

 

Should always be the first thing to code so when things do go wrong (which they always do) you can check a log and point a finger.

 

 

Expand  

Yeah, yeah, I know.. Like I said.  Idiot!! :p 

 

I'm still blaming the DBA though, as he did the connection string for the production server! :p 

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • I don't get why this company needs to change sockets every 2 generations, I really don't. If AMD can deliver a superior product both in terms of performance and energy consumption on the same socket for multiple generations, why can't Intel. I don't like to wear the tinfoil hat, but come on, this stinks of backroom deals with motherboard manufacturers? New socket, new motherboards, more sales.
    • unlimited internet, not unlimited data lol sure you can go anywhere on there but it's gonna cost you to do it!
    • Intel's next-gen desktop processors leak with a whopping number of cores by Taras Buria Intel's third-generation Core Ultra processors for desktop computers are on the way. If the latest leak is true, we are up for some significant updates in the Core Ultra 300 Series. @g01d3nm4ng0 on X posted a list of Intel's upcoming desktop processors, which includes seven SKUs ranging from entry-level Core Ultra 3 models to the flagship Core Ultra 9 with a rather whopping number of cores. According to the leak, the top-end model will feature 52 cores without hyperthreading. However, those cores are not like "classic" cores found in HEDT chips like AMD's Threadrippers. Instead, those 52 cores are divided into three categories: performance, efficiency, and low-power. The upcoming Core Ultra 9 reportedly features 16 performance cores, 32 efficiency cores, and 4 low-power cores. The cheapest Core Ultra 3 has four of each, 12 cores in total. Low-power cores are new to Intel's desktop processors. The current Core Ultra 200 chips feature the same hybrid configuration that was introduced with the 12th-gen Alder Lake chips: performance cores for demanding tasks and efficiency cores for lightweight tasks (up to 24 cores). Now, Intel wants to improve the efficiency of its chips further by adding another set of low-power cores, which were first introduced in the first-gen Core Ultra mobile chips (Meteor Lake). @jaykihn0 adds that Intel Nova Lake-S will support 8000 MT/s memory speed by default and 32 PCIe Gen 5 lanes and 16 PCIe Gen 4 lanes, totalling 48 lanes in CPU + chipset configuration. SKU P Cores E Cores LP Cores TDP Core Ultra 9 16 32 4 150W Core Ultra 7 14 24 4 150W Core Ultra 5 8 16 4 125W Core Ultra 5 8 12 4 125W Core Ultra 5 6 8 4 125W Core Ultra 3 4 8 4 65W Core Ultra 3 4 4 4 65W Today's leak aligns with previous reports about the upcoming Intel Nova Lake-S processors. However, Intel enthusiasts should probably get ready to switch platforms again, as the Nova Lake-S generation is rumored to require a brand-new LGA1954 socket.
  • Recent Achievements

    • Explorer
      treker_ed went up a rank
      Explorer
    • Apprentice
      CHUNWEI went up a rank
      Apprentice
    • Veteran
      1337ish went up a rank
      Veteran
    • Rookie
      john.al went up a rank
      Rookie
    • Week One Done
      patrickft456 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      656
    2. 2
      ATLien_0
      272
    3. 3
      +FloatingFatMan
      176
    4. 4
      Michael Scrip
      157
    5. 5
      Steven P.
      136
  • Tell a friend

    Love Neowin? Tell a friend!