Jquery 使用字典参数发布到操作
我感觉似曾相识,但我找不到答案: 我有一个对象数组,在检查 jQ $.post 调用时需要看起来像这样:
limiter[0].Key
limiter[0].Value
以便它在操作中映射
public ActionResult SomeAction(Dictionary<Guid, string> dictionary) { }
但是,这个 javascript:
// Some Guid and Some Value
var param = [ { 'Key' : '00000000-0000-00000-000000', 'Value': 'someValue' } ];
$.post('/SomeController/SomeAction/',
{
dictionary: limiter,
otherPostData: data
},
function(data) {
callback(data);
}
)
在 firebug 中检查它时会产生这个:
limiter[0][Key] = someKey // Guid Value
limiter[0][Value] = someValue
这是在 jq 1.4.2 中。我似乎记得你需要设置一些标志才能在 jQ 中以不同的方式呈现 json。这是否敲响了警钟?
I am feeling dejavu, but I cannot find the answer to this:
I have an array of objects that needs to look like this when inspecting a jQ $.post call:
limiter[0].Key
limiter[0].Value
so that it is mapped in the action
public ActionResult SomeAction(Dictionary<Guid, string> dictionary) { }
However, this javascript:
// Some Guid and Some Value
var param = [ { 'Key' : '00000000-0000-00000-000000', 'Value': 'someValue' } ];
$.post('/SomeController/SomeAction/',
{
dictionary: limiter,
otherPostData: data
},
function(data) {
callback(data);
}
)
produces this when inspecting it in firebug:
limiter[0][Key] = someKey // Guid Value
limiter[0][Value] = someValue
This is in jq 1.4.2. I seem to remember some flag you need to set to render json a different way in jQ. Does this ring any bells?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您将看到 post 参数为
limiter[0][Key]
因为 jquery 在发布之前会序列化 json 数据。控制器操作可以很好地解释这一点,并且您可以在操作中获得所需的输入。You will see the post param as
limiter[0][Key]
because jquery serializes the json data before it posts it. This is very well interpreted by the controller action and you get the required input in the action.您还可以使用对象列表,结果将与您想要的相同。这是一个很好的例子。
http:// /www.mikesdotnetting.com/Article/96/Handling-JSON-Arrays-returned-from-ASP.NET-Web-Services-with-jQuery
You can also use a list of objects and the result will be the same as what you wanted. This is a nice example.
http://www.mikesdotnetting.com/Article/96/Handling-JSON-Arrays-returned-from-ASP.NET-Web-Services-with-jQuery
尝试这样:
应该映射到以下控制器操作:
Try like this:
should map to the following controller action:
只需将您的 obj 作为 dataType: "json" 发送
just send your obj as dataType: "json"
您可以使用此标志 -
让 jQuery 以与您所看到的格式不同的格式发布数据。请参阅此问题以获取更多信息 -
使用 jQuery 在 ajax 调用中传递数组1.4
You can use this flag -
To get jQuery to post the data in a different format to the one you are seeing. See this question for further info -
Passing arrays in ajax call using jQuery 1.4