简体   繁体   English

可以默认模型联编程序生成IEnumerable <FormItem> ?

[英]can default model binder generate IEnumerable<FormItem>?

server: 服务器:

public class FormItems
{
    public IEnumerable<MyClass> Values { get; set; }
}

client: 客户:

<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>

and finally AJAXify the form: 最后使用AJAXify表单:

$(function() {
    $('#myform').submit(function() {
        var form = $(this);
        $.ajax({
            url: form.attr('action'),
            type: form.attr('method'),
            data: form.serialize(),
            success: function(result) {

            }
        });
    });
});

How can I use default model binder get the ajax data into strongly typed IEnumerable? 如何使用默认模型联编程序将Ajax数据转换为强类型的IEnumerable?

[HttpPost]
public JsonResult Save(FormItems data)

Assuming MyClass is something like this 假设MyClass是这样的

public class MyClass
{
     public string Value { get; set; }
}

Your html should look like this (note that name of each value input is prefixed by matching enumerable property name in FormItems ) 您的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM