1

在我的 ASP MVC 控制器中,我调用一个 Web 服务来处理一些信息。如果没有返回错误消息,那么我希望能够显示成功消息但也重置整个字段。

我曾认为最好的方法是String.IsNullOrWhiteSpace()在错误消息上使用,如果消息为空,请使用new语句擦除模型对象中的值,如下所示:

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

        return View(agtPay);

当我在 Visual Studio 中单步执行代码时,视图中的字段似乎都是null它们应有的样子,但是当页面加载时,以前的值仍然存在。认为这与缓存有关,我将以下标签放在视图顶部。

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

这是视图中的代码,带有缩写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);
    }
4

1 回答 1

1

这里的问题是初始请求是 a POST,所以Razor要从ModelState. 但是,您可以通过在返回之前发出此行来覆盖它:

ModelState.Clear();

之后,视图应该从您发回的模型中提取值。

但是,我将建议您更恰当地发出 a RedirectToAction,因为在我看来您只是想回到该GET州:

return RedirectToAction("AgentInformation");

代替:

return View(agtPay);
于 2013-09-25T22:33:17.423 回答