代码之家  ›  专栏  ›  技术社区  ›  Scott

RedirectToAction工作不正常

  •  0
  • Scott  · 技术社区  · 16 年前

    我有一个简单的MVC2应用程序,似乎没有正确重定向。代码设置如下:

        [HttpPost]
        [Authorize]
        public ActionResult QuickAddEvent(CalendarEvent calEvent)
        {
            if (ModelState.IsValid)
            {
                int eventID = repo.AddEvent(calEvent);
                return RedirectToAction("Event", new { id = eventID });
            }
    
            return RedirectToAction("Index", "Home");
        }
    
        [ChildActionOnly]
        public ActionResult QuickAddEvent()
        {
            return PartialView();
        }
    
        public ActionResult Event(int id)
        {
            CalendarEvent curEvent = repo.ByID(id);
            return View(curEvent);
        }
    

    我遇到的问题是,无论httppost上的modelstate是什么,页面都会重定向到自身。也就是说,无论模型状态是什么,我总是以/eventcalendar/index结束,而不是两个指定操作中的一个。

    1 回复  |  直到 16 年前
        1
  •  1
  •   Scott    16 年前

    由于quickaddevent正在返回partialview,因此表单操作将定位到/eventcalendar/index,而不是/eventcalendar/quickaddevent。修复程序正在将[httppost]的操作名更改为index