Ajax数据传输器

时间:2013-10-16 12:24:28

标签: javascript asp.net-mvc-3 razor model

我尝试使用Ajax Call($ .ajax)将ASP.NET MVC RAZOR中的数据列表发送到ActionResult(在Controller上),在控制器(服务器端)上,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]);
}

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

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

1 个答案:

答案 0 :(得分:1)

本文一定会帮助您MVC Model Binding to List of Complex Objects

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