2013-09-25 63 views
1

在我的ASP MVC控制器中,我調用一個Web服務來處理一些信息。如果沒有錯誤信息返回,我希望能夠顯示成功消息,但也重置整個字段。從控制器返回null模型對象以查看以清除字段

我原本以爲要做到這一點的最好辦法是對錯誤信息使用String.IsNullOrWhiteSpace(),如果該消息是空的,用new語句刪除模型對象中的值,像這樣:

 if (String.IsNullOrWhiteSpace(ViewBag.ErrorMessage)) 
     { 
      agtPay = new AgentInformation(); 
     } 

     return View(agtPay); 

當我瀏覽Visual Studio中的代碼時,視圖中的字段看起來應該都是null,但是當頁面加載之前的值仍然存在時。我認爲這有點做w /緩存我把下面的標籤放在視圖的頂部。

<meta http-equiv="PRAGMA" content="NO-CACHE"> 

下面是從視圖中的代碼W /縮寫switch聲明:

[HttpPost] 
    public ActionResult AgentInformation(AgentInformation agtPay) 
    { 
     //redirect if security is not met. 
     if (!Security.IsAgent(User)) return RedirectToAction("Message", "Home", new { id = 1 }); 

     try 
     { 
      //Run field validation and if anything found send back 
      string msg = AgentInformationValidator.ValidateFields(agtPay); 


      if (!String.IsNullOrWhiteSpace(msg)) 
      { 
       ViewBag.ErrorMessage = msg; 
       return View(agtPay); 
      } 

      //If valid send agtPay to web service 
      AgentsClient webService = new AgentsClient(); 
      ReturnMessage returnMessage = new ReturnMessage(); 

      string userName = User.Identity.Name; 
      userName = userName.Substring(Math.Max(0, userName.Length - 6)); 

      switch (agtPay.TransactionType) 
      { 
       case ("E"): 
        returnMessage = webService.EftUpdateByTaxid(true, 
                   agtPay.RefNumType.ToString(), 
                   agtPay.RefNumber, 
                   agtPay.OwnerMasterAgentId, 
                   agtPay.PaymentFrequency, 
                   false, 
                   agtPay.RoutingNumber, 
                   agtPay.AccountType, 
                   agtPay.AccountNumber, 
                   REQUEST_SYSTEM, 
                   userName); 
        break; 
       . 
       . 
       . 
       . 
       case ("U"): 
        returnMessage = webService.UnenrollEft(agtPay.RefNumType.ToString(), 
                  agtPay.RefNumber, 
                  agtPay.OwnerMasterAgentId, 
                  REQUEST_SYSTEM, 
                  userName); 
        break; 
       default: 
        ViewBag.ErrorMessage = "There was an error processing your request: No transaction type specified"; 
        break; 
      } 

      if (returnMessage.Success) 
      { 
       string[] msgArray = returnMessage.FriendlyMsg.Split(';'); 
       ViewBag.ReturnMessage = msgArray[0]; 

       if (msgArray.Length > 0) 
       { 
        for (int i = 1; i < msgArray.Length; i++) 
        { 
         ViewBag.ErrorMessage += msgArray[i]; 
        } 
       } 
      } 
      else 
      { 
       ViewBag.ErrorMessage = returnMessage.FriendlyMsg; 
      } 
     } 
     catch (Exception ex) 
     { 
      EventLog.WriteEntry("Monet", "From Agent Controller: \n\r|Ex Message| " + ex.Message + "\n\r|Stack Trace| " + ex.StackTrace); 
      ViewBag.ErrorMessage = "An error has occurred, IT has been notified and will resolve the issue shortly!"; 
      SendEmail.ErrorMail(ex); 
     } 

     if (String.IsNullOrWhiteSpace(ViewBag.ErrorMessage)) 
     { 
      agtPay = new AgentInformation(); 
     } 

     return View(agtPay); 
    } 

回答

1

這裏的問題是,最初的請求是POST,所以Razor打算從ModelState抓住這些值。但是,您可以通過返回前發出這條線覆蓋它:

ModelState.Clear(); 

這樣做,認爲應該從你發回的模型拉動值後。

不過,我會建議你更恰當只是發出一個RedirectToAction,因爲在我看來,你只是試圖讓回到GET狀態:

return RedirectToAction("AgentInformation"); 

代替:

return View(agtPay); 
+1

太棒了,謝謝。 – NealR

相關問題