【发布时间】: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