Getting a “No parameterless constructor defined” e

2020-07-10 11:35发布

问题:

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.

回答1:

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?



回答2:

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.



回答3:

.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.



回答4:

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()
    {
        // ...
    }
}


回答5:

public class MyController : Controller
{
    // make sure constructor has default parameterless constructor
    public MyController() {}
}


回答6:

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.



回答7:

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.