使用RedirectToAction时转发ModelState错误消息

时间:2014-03-24 10:52:46

标签: asp.net-mvc asp.net-mvc-routing modelstate redirecttoaction

在我的控制器功能的Post方法中,我添加了一些ModelStateError。添加后,我重定向到Get方法。 在我返回视图的Get方法中,我希望能够获取从Post方法传递的错误消息并将其添加到Modelstate。

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult AttendeeAvailability(params params)
    {            
        ...
        ...
        if (some statement)
        {
            ModelState.AddModelError(string.Empty,Resource.message);
            return RedirectToAction("someaction", new { response.AppointmentId, response.AttendeeId });
        }

        return RedirectToAction("someaction", "somecontroller");
    }

现在添加了错误,我想在以下函数中检索它

    [HttpGet]
    public ActionResult AttendeeAvailability(Guid appointmentId,Guid attendeeId)
    {
        .....
        Modelstate.AddModelError()//add message passed from post(if any);
        return View(model);
    }

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

然后你必须使用TempData或session。向其添加错误消息并将其显示在视图中。

//In Controller
string text = TempData["text"] as string;
//In View
@ViewContext.TempData["text"]