服务器:
public class FormItems
{
public IEnumerable<MyClass> Values { get; set; }
}
客户端:
<form id="myform" action="/" method="post">
<!-- Those inputs could be added dynamically -->
<input type="text" name="[0].Value" />
<input type="text" name="[1].Value" />
<input type="text" name="[2].Value" />
<input type="text" name="[3].Value" />
<button type="submit">OK</button>
</form>
最后AJAX化表格:
$(function() {
$('#myform').submit(function() {
var form = $(this);
$.ajax({
url: form.attr('action'),
type: form.attr('method'),
data: form.serialize(),
success: function(result) {
}
});
});
});
如何使用默认模型绑定器将ajax数据转换为强类型IEnumerable?
[HttpPost]
public JsonResult Save(FormItems data)
答案 0 :(得分:1)
假设MyClass
是这样的
public class MyClass
{
public string Value { get; set; }
}
您的html应如下所示(请注意,每个值输入的名称都以FormItems
中匹配的可枚举属性名称为前缀)
< form id="myform" action="/" method="post">
<!-- Those inputs could be added dynamically -->
<input type="text" name="Values[0].Value" />
<input type="text" name="Values[1].Value" />
<input type="text" name="Values[2].Value" />
<input type="text" name="Values[3].Value" />
<button type="submit">OK</button>
</form>