Getting a “No parameterless constructor defined” e

2020-07-10 11:21发布

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.

7条回答
成全新的幸福
2楼-- · 2020-07-10 12:17

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

查看更多
登录 后发表回答