我是MVC的新手,我正在创建一个post方法,以便在jquery中选择一个datepicker之后显示新数据。 jquery方法正常工作,我可以循环浏览视图中的新数据,但它不会在浏览器中显示。这是我的帖子方法。
[HttpPost]
public ActionResult Birthdays(DateTime? pickedDate = null)
{
if (pickedDate == null)
{
startDate = DateTime.Now;
}
else
{
startDate = (DateTime)pickedDate;
}
ModelState.Clear();
List<OKSwingMemberList> birthdayList = db.OKSwingMemberLists.ToList();
ViewBag.birthDayList = birthdayList.Where(b => b.BirthMonth == startDate.Month.ToString()).Where(a => a.Anniversary > DateTime.Now).OrderBy(o => o.LastName);
return View();
}
答案 0 :(得分:0)
Viewbag仅在页面加载时加载(在服务器上),而ajax是为客户端呈现而设计的。您需要通过json而不是Viewbag传递数据:
类似的东西:
birthdayList = birthdayList.Where(b => b.BirthMonth == startDate.Month.ToString()).Where(a => a.Anniversary > DateTime.Now).OrderBy(o => o.LastName))
return Json(new {BirthdayList = birthdayList});
然后使用类似的东西在js中访问它:
success: function (data) {
var bithdayList = data.BirthdayList;
//do your stuff
}