帮助使用 jquery .ajax() 方法将聚合数据传递给 mvc2 操作
我正在尝试使用 jquery ajax 函数发布到 MVC2 控制器操作,但我的参数包括一个(简单)自定义类的数组,并且该操作无法正确获取数据。
客户端:
var element1 = { FirstName: 'Raymond', LastName: 'Burr' };
var element2 = { FirstName: 'Johnny', LastName: 'Five' };
var var2 = [element1, element2];
var var1 = 'some string';
var parms = {
var1: var1,
var2: var2
};
var ajaxArgs = {
type: "POST",
traditional: true,
url: "/Home/Test1",
data: parms,
dataType: "json",
success: returnSuccess,
error: returnError
};
$.ajax(ajaxArgs);
服务器:
[HttpPost]
public ActionResult Test1(string var1, List<TestParameterClass> var2) { ... }
public class TestParameterClass
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
2 个已经工作的案例: 1) 使用List<_string>作为操作参数并将 javascript 数组更改为字符串 数组。
2) 使用 TestParameterClass 作为操作参数并传递 1 个自定义类的实例。
因此,真正的技巧似乎是获取成功传递的自定义类的数组以及其他平面(字符串)参数。
有什么想法可以让这项工作成功吗?还有关于 MVC2 如何将参数转换为某种 C# 类型的文档吗(我仅使用过 List<> b/c 它似乎是使用最广泛的)?
谢谢!
I'm trying to use the jquery ajax function to post to a MVC2 controller action, but my parameters include an array of a (simple) custom class and the action is not getting the data correctly.
Client:
var element1 = { FirstName: 'Raymond', LastName: 'Burr' };
var element2 = { FirstName: 'Johnny', LastName: 'Five' };
var var2 = [element1, element2];
var var1 = 'some string';
var parms = {
var1: var1,
var2: var2
};
var ajaxArgs = {
type: "POST",
traditional: true,
url: "/Home/Test1",
data: parms,
dataType: "json",
success: returnSuccess,
error: returnError
};
$.ajax(ajaxArgs);
Server:
[HttpPost]
public ActionResult Test1(string var1, List<TestParameterClass> var2) { ... }
public class TestParameterClass
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
2 cases which already work:
1) Using a List<_string> as action parameter and changing the javascript array to a string
array.
2) Using a TestParameterClass as an action parameter and passing 1 instance of the custom class.
So the real trick seems to be getting an array of a custom class passed successfully and with other flat (string) parameters.
Any ideas to make this work? Also is there any documentation on how MVC2 translates the parameter to some C# type (I've used List<> only b/c it seems the most widely used)?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
啊,模型绑定器。当谈到 .NET MVC 时,这是如此多魔力的源泉,也是如此多灾难的根源。模型绑定器将参数转换为对象,有时它对传递给它的内容有点挑剔。
在这种特殊情况下,我认为您想要的是以形式传递参数
基本上,如果您向模型绑定器传递集合类型,它希望参数以数组样式索引(括号号)命名,然后属性名称。至少这对我有用;我不是 MVC 忍者。
Ah, the Model Binder. The source of so much magic and the cause of so much woe when it comes to .NET MVC. The Model Binder is what turns parameters into objects, and sometimes it's a little picky about what you pass it.
In this particular case, I think what you want is to pass parameters in the form
Basically, if you're passing the model binder a collection type, it wants the parameters to be named with array-style indices (bracket-number) and then the property name. At least that's what's worked for me; I'm no MVC ninja.
我会尝试找出 AJAX 调用是否错误或者服务器上的输入模型绑定是否错误。使用 Firebug 或 Fiddler 检查 AJAX 调用,特别是 POST 数据。如果看起来正确,则问题可能出在模型绑定中。
查看这篇文章以获取列表解析的一些“文档”:
http://haacked.com/archive/2008 /10/23/model-binding-to-a-list.aspx
另一件要尝试的事情是创建一个示例页面,其表单模仿您通过 AJAX 传递的数据。如果您可以将表单正确地进行 POST 和模型绑定,那么您的 AJAX 调用很可能也能正常工作。
I would try figure out if the AJAX call is wrong or if the input model binding on the server is wrong. Use Firebug or Fiddler to inspect the AJAX call and specifically the POST data. if it looks correct, then the problem is likely in the model binding.
check out this post for some "documentation" of the list parsing:
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
another thing to try is to create a sample page with a form that mimics that data you're passing in via AJAX. if you can get the form to POST and model bind correctly, chances are your AJAX call will work as well.