zwei1121

博客园 首页 新随笔 联系 订阅 管理
$(document).ready(function (){var things =[{ id:1, color:'yellow'},{ id:2, color:'blue'},{ id:3, color:'red'}];      

    things = JSON.stringify({'things': things });

    $.ajax({
        contentType:'application/json; charset=utf-8',
        dataType:'json',
        type:'POST',
        url:'/Home/PassThings',
        data: things,
        success: function (){          
            $('#result').html('"PassThings()" successfully called.');},
        failure: function (response){          
            $('#result').html(response);}});});publicvoidPassThings(List<Thing> things){var t = things;}publicclassThing{publicintId{ get;set;}publicstringColor{ get;set;}}


<form><input name="ListThing" value="one"/><input name="ListThing" value="two"/><input name="ListThing" value="three"/><input name="Name" value="Bob"/></form><form><input name="ListThing" value="one"/><input name="ListThing" value="two"/><input name="ListThing" value="three"/><input name="Name" value="Pat"/></form>

and then assuming you have some link to trigger the action:

@Html.ActionLink("Save all","SaveAll","Home",null,new{ id ="saveall"})

you could simply AJAXify it:

$('#saveall').click(function (){var data = $('form').map(function (){var $form = $(this);return{
            name: $form.find(':input[name=Name]').val(),
            listThing: $form.find(':input[name=ListThing]').map(function (){return $(this).val();}).toArray()};}).toArray();

    $.ajax({
        url:this.href,
        type:'post',
        contentType:'application/json',
        data: JSON.stringify({ myModels: data }),
        success: function (result){
            alert('done');}});returnfalse;});



http://stackoverflow.com/questions/13242414/passing-a-list-of-objects-into-an-mvc-controller-method-using-jquery-ajax
http://stackoverflow.com/questions/5502675/asp-net-mvc-and-jquery-serialize-multiple-forms-to-list-of-objects-and-submit
http://blog.sina.com.cn/s/blog_4f925fc30100la41.html
 
posted on 2014-04-09 22:32  zwei  阅读(2142)  评论(0编辑  收藏  举报