-1

在我的控制器功能的Post方法中,我添加了一些ModelStateError。一旦添加,我重定向到Get方法。 在Get方法返回視圖時,我希望能夠獲取從Post方法傳遞的錯誤消息並將其添加到Modelstate。在使用RedirectToAction時轉發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); 
    } 

任何建議,以找回它?

+0

我不認爲ModelState錯誤可以傳遞到不同的動作。 Whate @Pratik建議可以處理這種情況。這是我能找到的[post](http://stackoverflow.com/questions/4642845/asp-net-mvc-how-to-preserve-modelstate-errors-across-redirecttoaction)。 – Nilesh

回答

0

然後,您必須使用TempData或會話。向其添加錯誤消息並將其顯示在視圖中。

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

我必須重定向,因爲我初始化一些視圖 – Cybercop

+0

所需的ViewModels參數,當if語句內的return語句執行時,modelState的錯誤不會傳遞給動作,它將重定向到 – Cybercop

+0

我已更改答案,請看一看在它 –

相關問題