将null模型对象从控制器返回到视图以清除字段

时间:2013-09-25 22:02:32

标签: c# asp.net-mvc asp.net-mvc-3

在我的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);
    }

1 个答案:

答案 0 :(得分:1)

此处的问题是初始请求为POST,因此Razor将从ModelState中获取这些值。但是,您可以在返回之前通过发出此行来覆盖它:

ModelState.Clear();

执行此操作后,视图应从您发回的模型中提取值。

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

return RedirectToAction("AgentInformation");

而不是:

return View(agtPay);