troubleshooting Question

Passing Error Message from Controller to ViewBag

Avatar of Crystal Rouse
Crystal RouseFlag for United States of America asked on
C#* asp.net core mvc
6 Comments1 Solution118 ViewsLast Modified:
Trying to figure out how to get a error message to the View Bag.  I have this in my View:

 @if (!string.IsNullOrEmpty(ViewBag.Message))
    {
        <script type="text/javascript">
        swal({
            title: "Error",
            text: "@ViewBag.Message",
            type: "error"
        }, function () {
            url = '@Url.Action("Index", "Requests")';
            window.location.href = url;
        });
        </script>
    }

My Controller has both a Get and a Post and I'm not sure how to get the error message to my View in the ViewBag.Message

[HttpGet]
        public ActionResult Index()
        {

            ViewBag.Message = String.Empty;
            if (!String.IsNullOrEmpty(HttpContext.Session["Error"]?.ToString()))
            {
                ViewBag.Message = HttpContext.Session["Error"].ToString();
                HttpContext.Session.Remove("Error");
            }

            return View();
        }

 [HttpPost]
        public ActionResult Upload(HttpPostedFileBase file, string Name)
        {
            var result = new { Success = "", Message = "" };
            try
                {
                    string pdfTemplate = "NewRequest.pdf";
                    byte[] pdfbytes = null;
                    BinaryReader rdr = new BinaryReader(file.InputStream);
                    pdfbytes = rdr.ReadBytes((int)file.ContentLength);
                    PdfReader reader = new PdfReader(pdfbytes);

                    string t = Name.Trim().Length == 0 ? null : Name;
                    var fields = reader.AcroFields.Fields;
                    var RequestID = reader.AcroFields.GetField("requestId");
                    var OpenDate = reader.AcroFields.GetField("openDate");
                    var Org = reader.AcroFields.GetField("organization");
                    var NeedDate = reader.AcroFields.GetField("needDate");
                    var FirstName = reader.AcroFields.GetField("firstName");
                    var LastName = reader.AcroFields.GetField("lastName");
                     .....
                  
                    tbl_Request request = new tbl_Request()
                    {
                        addedDate = DateTime.Now,
                        title = t,
                        requestId = RequestID,
                        openDate = Convert.ToDateTime(OpenDate),
                        organization = Org,
                        needDate = Convert.ToDateTime(NeedDate),
                        firstName = FirstName,
                        lastName = LastName

                       ..........
                    };

                    myDB.tbl_Request.Add(request);
                    myDB.SaveChanges();
                }
                catch (Exception ex)
                {
                    Utility util = new Utility();
                    string error = util.innerExcep(ex);
                result = new { Success = "False", Message = error };
                return RedirectToAction("Index", "Upload", new { Result = result });
                }

            return RedirectToAction("Index", "Requests", new { });
        }
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros