【发布时间】:2015-11-04 02:02:28
【问题描述】:
我正在尝试将一组对象发布到控制器中的操作。如果我使用常规提交按钮发布,一切都会按预期工作,但是当我尝试通过 Ajax 发布时,列表总是空的。有任何想法吗?我的代码如下。
查看:
@using (Html.BeginForm("Save", "Home", FormMethod.Post, new { id = "myform" }))
{
<input type="text" name="Childs[0].Name" value="Name 1" />
<input type="text" name="Childs[0].Age" value="12" />
<input type="text" name="Childs.Index" value="0" />
<input type="text" name="Childs[1].Name" value="Name 2" />
<input type="text" name="Childs[1].Age" value="23" />
<input type="text" name="Childs.Index" value="1" />
<input type="text" name="AnotherProperty" value="111" />
<input type="button" onclick="PostForm()" value="Test" />
}
型号:
public class BinderTestModel
{
public BinderTestModel()
{
Childs = new List<BinderTestChildModel>();
}
public int AnotherProperty { get; set; }
public List<BinderTestChildModel> Childs { get; set; }
}
public class BinderTestChildModel
{
public string Name { get; set; }
public int Age { get; set; }
}
JS:
function PostForm()
{
$.ajax({
url: '@Url.Action("Save")',
type: "POST",
contentType: 'application/json; charset=utf-8',
datatype:'json',
data: JSON.stringify($("#myform").serialize()),
success: function()
{
},
error: function (jqXHR, exception) {
alert('Error message.');
}
});
}
谢谢,
贡萨洛
【问题讨论】:
标签: .net json asp.net-mvc