Ajax 数据传输器

Ajax data transporter

本文关键字:传输器 数据传输 数据 Ajax      更新时间:2023-09-26

我尝试使用Ajax Call($.ajax)将数据列表从MVC RAZOR ASP.NET 发送到ActionResult(在控制器上),在控制器(服务器端)上,ActionResult 的参数为NULL。

//ajax call
     $.ajax({
        url: "@Url.Content("~/DayPartConfig/Index")",
        type: "POST",
        cache: false,
        data: myItems,
        success: function(data){                 
               alert('success');          
        },
        error: function () {
            alert('Error updating the time interval.');
        },
        complete: function(){
            //hide preloader   
            alert('preloader');    
        }
    });

myItems的所有权名称和结构与Daypart相同:

 public partial class DayPart
    {
        public DayPart()
        {
            this.EventGoalDayParts = new HashSet<EventGoalDayPart>();
        }
        public int DayPartID { get; set; }
        public int Position { get; set; }
        public string Name { get; set; }
        public bool IsEnable { get; set; }
        public Nullable<System.TimeSpan> Start { get; set; }
        public Nullable<System.TimeSpan> End { get; set; }
        public virtual ICollection<EventGoalDayPart> EventGoalDayParts { get; set; }
    }

从JavaScript:

for (i = 1 ; i<= @Model.Count()-1;i++)
{
 CreatedItem = {'DayPartID': i,
                'Position': i,
                'Name': $("#Name_"+i).val(),
                'IsEnable': $("#IsEnable_"+i).val(),
                'Start': $('#timepicker-'+ i).val()
                };
    myItems[i] = CreatedItem;
    //alert(myItems[i]);
}

现在,为什么来自控制器的模型是零????

 [HttpPost]
    public ActionResult Index(List<DayPart> model)
    {
...
}
本文

肯定会帮助您MVC Model Binding to List of Complex Objects

http://seesharpdeveloper.blogspot.in/2012/05/mvc-model-binding-to-list-of-complex.html