我尝试使用 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)
{
...
}