Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a "No parameterless constructor defined" error, not sure why

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.

like image 752
Jay Sun Avatar asked Dec 13 '11 21:12

Jay Sun


1 Answers

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?

like image 164
Scott Rippey Avatar answered Oct 15 '22 05:10

Scott Rippey