【问题标题】:Ajax data transporterAjax 数据传输器
【发布时间】:2013-10-16 21:11:39
【问题描述】:

我尝试使用 Ajax 调用 ($.ajax) 将 ASP.NET MVC RAZOR 中的数据列表发送到 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)
    {
...
}

【问题讨论】:

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


    【解决方案1】:

    这篇文章一定能帮到你MVC Model Binding to List of Complex Objects

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

    【讨论】:

      猜你喜欢
      • 2017-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-18
      • 2014-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多