简体   繁体   中英

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:

$(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?

[HttpPost]
public JsonResult Save(FormItems data)

Assuming MyClass is something like this

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 )

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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