admin管理员组文章数量:1287598
I have the following javascript class which I am trying to pass to an ASP.NET MVC Controller
var postParams = {
attributes : [
{
name : '',
description: '',
attributeType : '',
value : ''
}
]
};
postParams.attributes[0] = new Object();
postParams.attributes[0].name = "test";
postParams.attributes[0].description = "test";
postParams.attributes[0].attributeType = "test";
postParams.attributes[0].value = "test";
Here's how I call the Controller method:
var actionURL = "@Url.Action("Save", "Catalog")";
$.ajax({
type: "POST",
url: actionURL,
data: postParams ......
On the Controller side I've declared a ViewModel as follows:
public class AttributeViewModel
{
public string name { get; set; }
public string description { get; set; }
public string attributeType { get; set; }
public string value { get; set; }
}
My Controller Save method is declared as follows:
public JsonResult Save(AttributeViewModel[] attributes)
When I execute the value of attributes is always null.
Any ideas? Not sure how to even start debugging this.
I have the following javascript class which I am trying to pass to an ASP.NET MVC Controller
var postParams = {
attributes : [
{
name : '',
description: '',
attributeType : '',
value : ''
}
]
};
postParams.attributes[0] = new Object();
postParams.attributes[0].name = "test";
postParams.attributes[0].description = "test";
postParams.attributes[0].attributeType = "test";
postParams.attributes[0].value = "test";
Here's how I call the Controller method:
var actionURL = "@Url.Action("Save", "Catalog")";
$.ajax({
type: "POST",
url: actionURL,
data: postParams ......
On the Controller side I've declared a ViewModel as follows:
public class AttributeViewModel
{
public string name { get; set; }
public string description { get; set; }
public string attributeType { get; set; }
public string value { get; set; }
}
My Controller Save method is declared as follows:
public JsonResult Save(AttributeViewModel[] attributes)
When I execute the value of attributes is always null.
Any ideas? Not sure how to even start debugging this.
Share Improve this question edited Jul 14, 2012 at 14:00 tereško 58.4k25 gold badges100 silver badges150 bronze badges asked Apr 6, 2012 at 11:05 LanceLance 4912 gold badges6 silver badges20 bronze badges 02 Answers
Reset to default 6You can try json library to solve your issue
[JsonFilter(Param = "attributes", JsonDataType = typeof(AttributeViewModel[]))]
public JsonResult Save(AttributeViewModel[] attributes)
At client:
$.ajax({
type: 'POST',
url: url,
async: true,
data: JSON.stringify(attributes), //!!! Here must be the same name as in controller method
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function (data) {
},
error: function (xhr, ajaxOptions, thrownError) {
}
});
Seems like you need to add traditional : true
to the ajax call. Take a look here and here
$.ajax({
traditional: true,
type: "POST",
url: actionURL,
data: postParams
本文标签: Passing Javascript array to ASPNET MVC ControllerStack Overflow
版权声明:本文标题:Passing Javascript array to ASP.NET MVC Controller - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741219452a2360684.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论