1

这是我的代码,我想将一些数据(此处的日历日期)发布到“home”控制器的“index”操作,并希望将这些数据保存在数据库中,同时我想重定向到另一个操作控制器,即“主页”控制器的“索引”动作

这是我下面的jquery代码,

  function getDate() {

            $('.cal_daybox').on('click', function () {

           var timestamp = parseInt($(this).attr('id'));

                var day = new Date(timestamp);

                alert("You clicked " + day.toDateString());

                var url = '@Url.Content("~/Home/Index")';
                var date = day.toDateString();
                $.ajax({
                    type: "POST",
                    url: url,
                    data: { date: day.toDateString() },
                    dataType:"json"
                });                  
             return false;
            });  

事件控制器.cs

  public ActionResult Index()
    {
        return View(db.Events.ToList());
    }

家庭控制器.cs

[HttpPost]
    public ActionResult Index(DateTime date)
    {

            Date dt = new Date();
            dt.StartDate = date;
            db.Dates.Add(dt);   
            db.SaveChanges();
            return RedirectToAction("Index", "Events", new { id = dt.DateID });
    }
4

2 回答 2

0

您的事件控制器的索引操作没有接收 Dateid 值的参数。它无法找到可以接收 DateId val 的操作。

您需要修改现有的动作或像这样添加动作重载

public ActionResult Index(DateTime dateId)
{
    //Do something with the val
}

public ActionResult Index()
{
    return View(db.Events.ToList());
}
于 2013-05-04T07:53:26.173 回答
0
you are calling controller action using ajax so in this case you have to change you action method and jquery call like this:




         [HttpPost]
         public ActionResult Index(DateTime date)
         {

            Date dt = new Date();
            dt.StartDate = date;
            db.Dates.Add(dt);   
            db.SaveChanges();
            return Json(dt.DateID,JsonAllowBehaviour.AllowGet);
          // return RedirectToAction("Index", "Events", new { id = dt.DateID });
         }
    function getDate() {

            $('.cal_daybox').on('click', function () {

           var timestamp = parseInt($(this).attr('id'));

                var day = new Date(timestamp);

                alert("You clicked " + day.toDateString());

                var url = '@Url.Content("~/Home/Index")';
                var date = day.toDateString();
                $.ajax({
                    type: "POST",
                    url: url,
                    data: { date: day.toDateString() },
                    dataType:"json",
                    onsuccess:function(id)
                                 {
window.location='/Events/Index?Id='+id;
                                  }
                });                  
             return false;
            });  


And also please change your index action of Events Controller like below:
public ActionResult Index(int Id=0)
{
    return View(db.Events.ToList());
}
于 2014-02-11T14:27:55.313 回答