6

For some reason one particular AJAX call of mine is getting a "No parameterless constructor defined" error. Here's the code:

CallAndReplace(JSON.stringify(model), url, $("#panel"));

function CallAndReplace(data, url, replace) {
    $.ajax({
        url: url,
        type: "post",
        contentType: "application/json; charset=utf-8",
        data: data,
        success: function (result) {
            replace.html(result);
        },
        error: function (x, e) {
            if (x.status == 0) {
                alert('You are offline!!\n Please Check Your Network.');
            } else if (x.status == 404) {
                alert('Requested URL not found.');
            } else if (x.status == 500) {
                alert('Internal Server Error.');
            } else if (e == 'parsererror') {
                alert('Error.\nParsing JSON Request failed.');
            } else if (e == 'timeout') {
                alert('Request Time out.');
            } else {
                alert('Unknow Error.\n' + x.responseText);
            }
        }
    });
}

'model' is a viewmodel in my MVC-3 view that I've converted into a Javascript object. 'url' is the url generated via the '@Url.Action("Action", "Controller")' method. And $("#panel") is the div area that gets replaced by a partial view returned by the controller action.

When I try to debug the project, it never gets to the controller action. When I created a dummy controller action with no parameters, it reaches there in debug mode. But I'm obviously sending data. I can see the data being posted in Firebug (although it's not structured for some reason) but apparently it's not being sent over and I don't know why.

I use CallAndReplace 20 other times in my code for other uses and it has never given me this problem. I am completely at a loss as to why.

Edit: Here's the viewmodel class that I'm sending to the view:

public class AwardsEdit
{
    public List<AwardsViewModel> Awards { get; set; }
    public int TitleId { get; set; }

    public List<Tuple<int, string>> Participants { get; set; }
    public List<Award1> AllAwards { get; set; }
    public List<Tuple<int, string>> AllAwardCompanies { get; set; }
}

And the controller action I'm trying to call:

public PartialViewResult SaveAwards(AwardsEdit award)
    {
        if (ModelState.IsValid)
        {
            bool updated = _translator.UpdateAward(award);
            if (updated)
            {
                return PartialView("Details", _translator.GetAwards(award.TitleId));
            }
            //else error
            ModelState.AddModelError("", "Award data was not saved.");
        }
        //on error, load meta data
        var data = _translator.GetAwards(award.TitleId, true);

        award.Participants = data.Participants;
        award.AllAwards = data.AllAwards;
        award.AllAwardCompanies = data.AllAwardCompanies;

        return ViewAwards(award.TitleId);
    }

The controller itself doesn't have a parameterless constructor method and I am using dependency injection, but I have other AJAX calls that call various actions in that controller and they work fine. I don't know why this one isn't working.

Jay Sun
  • 1,583
  • 3
  • 25
  • 33
  • Can you post your controller code? Are you doing any dependency injection? I get this error when my controller does not have a default parameterless constructor. – danludwig Dec 13 '11 at 21:29
  • that error is server side, check to see if your `data` contains anything before you make the Ajax call, sounds like dependency on your controller is not working for that call – Kris Ivanov Dec 13 '11 at 21:30
  • Please post the code for the action in your controller that accepts your AJAX as well as the constructor(s) for the model it accepts. I suspect that you are accepting a Model/ViewModel of some type and your Model/ViewModel doesn't have a constructor that takes 0 arguments. When the ModelBinder in .NET tries to convert the incoming form to your model type it requires that your model have a constructor that takes 0 parameters. – Nick Bork Dec 13 '11 at 21:33
  • Just posted the action and view model. – Jay Sun Dec 13 '11 at 21:46
  • You answered your own question: "The controller itself doesn't have a parameterless constructor method and I am using dependency injection". You'll need to dig in to the dependency injection to figure out why that isn't working. – Nick Bork Dec 13 '11 at 21:50
  • Yes, but I have other AJAX calls to other actions in that same controller that use resources injected into the controller and they work. – Jay Sun Dec 13 '11 at 21:52
  • No two methods are exactly alike, use the debugger and walk the code. Your error is above the "action" or your "model", it lies in the construction of an instance of your controller. To figure it out you'll have to get your hands deep in to the variables and inspect why one call produces an instance of the controller when the other doesn't. – Nick Bork Dec 13 '11 at 21:57

7 Answers7

11

The error is probably referring to the type of one of your action's parameters (not the Controller, as others have suggested).
MVC cannot populate the parameter if it cannot first be constructed.

For example, you can get the same error like this:

public class ParameterConstructor
{
    public ParameterConstructor(string parameter){
    }
}

public class MyController : Controller {
    public ActionResult Test(ParameterConstructor model) {
        return "This action will not be reached";
    }
}

So you need to make sure that your model type has a parameterless constructor.

Update

In response to your updated code, it is true that your ViewModel constructor is parameterless.
However, you have a list of Tuple<int, string>. The documentation says that Tuple does not have a parameterless constructor. That's the problem -- Tuple is designed to be read-only. Perhaps you could use a KeyValuePair<> instead?

Scott Rippey
  • 15,614
  • 5
  • 70
  • 85
  • My other view models don't have parameterless constructors and sending them through AJAX works fine. – Jay Sun Dec 13 '11 at 21:45
  • @JaySun Interesting ... if the constructor isn't parameterless, then what parameters does MVC call it with? Can you set a breakpoint inside the constructor and see what values you get? I'm genuinely curious. – Scott Rippey Dec 14 '11 at 01:26
  • 1
    @JaySun It looks like `Tuple<>` does not have a parameterless constructor. That's probably the problem. I updated my answer accordingly. – Scott Rippey Dec 14 '11 at 01:36
  • Perfect thanks. I had `SelectList`, which didn't have a parameterless constructor so had to use `IEnumerable` – Ian Jul 30 '15 at 16:01
0

If you want to pass JSON to your server, you have to pass it as the value of a parameter. Just setting the "data" property on the AJAX call won't work.

You could use:

    data: { theJson: data },

then your server would see a parameter "theJSON" and could decode the value of that to get your data structure back.

Pointy
  • 405,095
  • 59
  • 585
  • 614
0

.stringify is the reason it is not structured. That call will take your data, add a [ to the front, a ] to the back, and then create a comma delineated string of the data you sent. Perhaps your controller expects an object or model and is instead getting a string.

Travis J
  • 81,153
  • 41
  • 202
  • 273
0

I would verify that the Controller referenced in your @Url.Action("Action", "Controller") actually has a parameterless constructor. Or it could be that you do not expect there to be an parameterless constructor in which case you must make sure that your IoC Container is properly configured to inject the necessary dependency.

It could also be the ViewModel itself in which case you should ensure that a parameterless (default) constructor exists on the ViewModel type.

Example:

public class Example
{
    // this is a parameterless constructor
    public Example()
    {
        // ...
    }
}
Phil Klein
  • 7,344
  • 3
  • 30
  • 33
0
public class MyController : Controller
{
    // make sure constructor has default parameterless constructor
    public MyController() {}
}
danludwig
  • 46,965
  • 25
  • 159
  • 237
0

That error message isn't the greatest -- it sends people to one of the more likely causes of your controller class not being instantiated. But it really should read "Controller construction and initialization failed. One likey cause is that there is not a parameterless constructor but you might have screwed it up some other way. Inner exception was . . ."

Best bet to catch it would be to run your code in debug mode, Visual Studio should pop you over to the exception when it occurs.

Wyatt Barnett
  • 15,573
  • 3
  • 34
  • 53
  • I did run my code in debug mode. Nothing happened. The error just appeared in my browser. – Jay Sun Dec 13 '11 at 21:41
  • Interesting. Did you look at the debug window, sometimes that will let you know. Requesting the AJAX url directly could help too, as will inspecting the result -- it might have the inner exception which is what you need to get at. – Wyatt Barnett Dec 13 '11 at 21:43
0

Since you're using Dependency Injection you'll need to dig in to your code to figure out why the dependency injector isn't creating an instance of the controller with the paramteres it requires.

Nick Bork
  • 4,831
  • 1
  • 24
  • 25