Jay*_*Sun 6 javascript asp.net ajax jquery asp.net-mvc-3
由于某种原因,我的一个特定的AJAX调用正在获得"无参数构造函数定义"错误.这是代码:
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);
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
'model'是我MVC-3视图中的一个视图模型,我已经转换为Javascript对象.'url'是通过'@ Url.Action("Action","Controller")'方法生成的网址.而$("#panel")是由控制器动作返回的部分视图替换的div区域.
当我尝试调试项目时,它永远不会进入控制器操作.当我创建一个没有参数的虚拟控制器动作时,它以调试模式到达那里.但我显然是在发送数据.我可以看到Firebug中发布的数据(尽管由于某些原因它没有结构化)但显然它没有被发送过,我不知道为什么.
我在我的代码中使用CallAndReplace 20以用于其他用途,它从未给我这个问题.我完全不知道为什么.
编辑:这是我发送到视图的viewmodel类:
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; }
}
Run Code Online (Sandbox Code Playgroud)
我试图调用的控制器动作:
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);
}
Run Code Online (Sandbox Code Playgroud)
控制器本身没有无参数构造函数方法,我使用依赖注入,但我有其他AJAX调用调用该控制器中的各种操作,它们工作正常.我不知道为什么这个不起作用.
Sco*_*pey 11
错误可能是指您的某个操作参数的类型(而不是其他人建议的控制器).
如果无法首先构造参数,MVC无法填充该参数.
例如,您可以像这样得到相同的错误:
public class ParameterConstructor
{
public ParameterConstructor(string parameter){
}
}
public class MyController : Controller {
public ActionResult Test(ParameterConstructor model) {
return "This action will not be reached";
}
}
Run Code Online (Sandbox Code Playgroud)
因此,您需要确保您的model类型具有无参数构造函数.
为了响应您更新的代码,您的ViewModel构造函数确实是无参数的.
但是,你有一个列表Tuple<int, string>. 文档说Tuple没有无参数构造函数.这就是问题 - 元组被设计为只读.也许你可以用一个KeyValuePair<>代替?
| 归档时间: |
|
| 查看次数: |
5749 次 |
| 最近记录: |