获得一个[否参数的构造函数定义"错误,不知道为什么不知道为什么、函数、定义、错误

2023-09-10 16:45:51 作者:初醒

由于某些原因的矿山得到一个无参数的构造函数定义的错误一个特定的AJAX调用。这里的code:

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);
            }
        }
    });
}

模式是我,我已经转换成一个Javascript对象MVC-3视图视图模型。 网址是通过@ Url.Action(行动,控制器)'方法生成的URL。和$(#面板)是DIV区域被替换由控制器操作返回的局部视图。

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

当我尝试调试项目,而不会继续控制器动作。当我创建了一个虚拟控制器的动作没有参数,它到达那里调试模式。但我明明发送数据。我可以看到数据被张贴在Firebug(尽管它不规整出于某种原因),但显然它没有被发送过,我不知道为什么。

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.

我在code作其他用途使用其他CallAndReplace 20倍,它从来没有给我这个问题。我完全不知所措,为什么。

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.

编辑:下面是我送的视图的视图模型类:

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);
    }

控制器本身没有参数的构造函数的方法,我使用依赖注入,但我必须调用该控制器的各种动作,他们做工精细等AJAX调用。我不知道为什么这个人是不工作。

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.

推荐答案

该错误可能指的是你的行动的参数(不控制器,正如其他人的建议)。 MVC无法填充参数,如果它不能先构成。

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.

在响应您的更新code,这是事实,你的视图模型构造器是无参数。 但是,你有列表元组LT; INT,字符串&GT; 。 该文件说,元组没有一个参数的构造函数。这就是问题 - 元组被设计为只读。也许你可以使用一个 KeyValuePair&LT;&GT; 而不是

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?